1. Display the current time:
function dateDigitToString(num){ return num < 10 ? '0' + num : num; } function get_time(){ var currentDate = new Date(), year = dateDigitToString(currentDate.getFullYear()), month = dateDigitToString(currentDate.getMonth() + 1), date = dateDigitToString(currentDate.getDate()), hour = dateDigitToString(currentDate.getHours()), minute = dateDigitToString(currentDate.getMinutes()), second = dateDigitToString(currentDate.getSeconds()), formattedDateString = year + '年' + month + '月' + date + '日 ' + hour + ':' + minute + ':' + second; return formattedDateString; }
in addition:
moment.js does not depend on any third-party library, and supports formats such as string, Date, timestamp, and array.
Like PHP's date() function, it can format date and time, calculate relative time, and obtain date and time after a specific time.
current time:
moment().format('YYYY-MM-DD HH:mm:ss'); //2014-09-24 23:36:09
Today's day of the week:
moment().format('d'); //3
Convert the current time to a Unix timestamp:
moment().format('X');
Relative Time:
Date in 7 days:
moment().add('days',7).format('YYYY year MM month DD day'); //October 01, 2014
Time after 9 hours:
moment().add('hours',9).format('HH:mm:ss');
moment.js provides rich documentation, and it can also create complex date-time applications such as calendar items. The most commonly used format in daily development is to format the time. The following is a table description of the commonly used formats.
format code | illustrate | return value example |
M | month as a number, without leading zeros | 1 to 12 |
MM | month as a number, with leading zeros | 01 to 12 |
MMM | month represented by three letter abbreviation | Jan to Dec |
MMMM | month, full text format | January to December |
Q | quarter | 1 to 4 |
D | day of the month without leading zeros | 1 to 31 |
DD | day of the month, with leading zeros | 01 to 31 |
d | day of the week, numerically | 0 to 6, 0 means Sunday, 6 means Saturday |
ddd | Three letters for the day of the week | Sun to Sat |
dddd | Day of the week, full text of the week | Sunday to Saturday |
w | week of the year | Such as 42: indicates the 42nd week |
YYYY | four-digit year in full | eg: 2014 or 2000 |
YY | year in two digits | eg: 14 or 98 |
A | Capital AM PM | AM PM |
a | lowercase am pm | am pm |
HH | Hour, 24-hour clock, with leading zeros | 00 to 23 |
H | 小时,24小时制,无前导零 | 0到23 |
hh | 小时,12小时制,有前导零 | 00到12 |
h | 小时,12小时制,无前导零 | 0到12 |
m | 没有前导零的分钟数 | 0到59 |
mm | 有前导零的分钟数 | 00到59 |
s | 没有前导零的秒数 | 1到59 |
ss | 有前导零的描述 | 01到59 |
X | Unix时间戳 | 1411572969 |
2.prototype
prototype是一个针对于某一类的对象的方法,而且特殊的地方便在于:它是一个给类的对象添加
方法的方法。
javascript中的每个对象都有prototype属性,Javascript中对象的prototype属性是返回对象类型原型的引用
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
employee.prototype.year='35'
var bill=new employee("bob","Engineer",1985);
console.log(bill.year)
输出:35.
3.constructor
var test=new Array(); if (test.constructor==Array) { document.write("This is an Array"); } if (test.constructor==Boolean) { document.write("This is a Boolean"); } if (test.constructor==Date) { document.write("This is a Date"); } if (test.constructor==String) { document.write("This is a String"); } 输出:This is an Array例2. 展示如何使用 constructor 属性:
function employee(name,job,born) { this.name=name; this.job=job; this.born=born; } var bill=new employee("Bill Gates","Engineer",1985); document.write(bill.constructor);
输出: function employee(name, job, born) {this.name = name; this.job = job; this.born = born;}