摘要
创建Date 实例用来处理日期和时间。
构造函数
new Date(); new Date(value); new Date(dateString); new Date(year, month, day, hour, minute, second, millisecond);
Note: 需要注意的是只能通过调用 Date 构造函数来实例化日期对象:以常规函数调用它(即不加 new 操作符)将会返回一个字符串,而不是一个日期对象。另外,不像其他JavaScript 类型,
Date 对象没有字面量格式。参数
-
value - 代表自世界协调时1970年1月1日00:00:00 经过的毫秒数。
-
dateString -
表示日期的字符串值。该字符串应该能被
Date.parse()方法识别(符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。
-
year -
代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用
1998, 而不要用98.
-
month - 代表月份的整数值从0(1月)到11(12月)。
-
date - 代表一个月中的第几天的整数值,从1开始。
-
hour - 代表一天中的小时数的整数值 (24小时制)。
-
minute - 分钟数。
-
second - 秒数。
-
millisecond - 表示时间的毫秒部分的整数值。
描述
- If no arguments are provided, the constructor creates a JavaScript
Dateobject for the current date and time according to system settings. - If at least two arguments are supplied, missing arguments are either set to 1 (if day is missing) or 0 for all others.
- The JavaScript date is based on a time value that is milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.
- The JavaScript
Dateobject provides uniform behavior across platforms. The time value can be passed between systems to represent the same moment in time and if used to create a local date object, will reflect the local equivalent of the time. - The JavaScript
Dateobject supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed. - Invoking JavaScript
Dateas a function (i.e., without the new operator) will return a string representing the current date and time.
属性
For properties available on Date instances, see Properties of Date instances.
-
Date.prototype -
允许为
Date实例对象添加属性。 - Date.length
-
Date.length的值是 7。这是该构造函数可接受的参数个数。
方法
For methods available on Date instances, see Methods of Date instances.
-
Date.now() - 返回自 1970-1-1 00:00:00 UTC 至今所经过的毫秒数。
-
Date.parse() - 解析一个表示日期的字符串,并返回从 1970-1-1 00:00:00 所经过的毫秒数。
-
Date.UTC() - 接受和构造函数最长形式的参数相同的参数(从2到7),并返回从 1970-01-01 00:00:00 UTC 开始所经过的毫秒数。
Date 实例
所有的 Date 实例都继承自 Date.prototype。修改 Date 构造函数的原型对象会影响到所有的 Date 实例。
方法
Getter
-
Date.prototype.getDate() - 根据本地时间返回指定日期对象的月份中的第几天(1-31)。
-
Date.prototype.getDay() - 根据本地时间返回指定日期对象的星期中的第几天(0-6)。
-
Date.prototype.getFullYear() - 根据本地时间返回指定日期对象的年份(四位数年份时返回四位数字)。
-
Date.prototype.getHours() - 根据本地时间返回指定日期对象的小时(0-23)。
-
Date.prototype.getMilliseconds() - 根据本地时间返回指定日期对象的微秒(0-999)。
-
Date.prototype.getMinutes() - 根据本地时间返回指定日期对象的分钟(0-59)。
-
Date.prototype.getMonth() - 根据本地时间返回指定日期对象的月份(0-11)。
-
Date.prototype.getSeconds() - 根据本地时间返回指定日期对象的秒数(0-59)。
-
Date.prototype.getTime() - 返回从1970-1-1 00:00:00 UTC(协调世界时)到该日期经过的毫秒数,对于1970-1-1 00:00:00 UTC之前的时间返回负值。
-
Date.prototype.getTimezoneOffset() - 返回当前时区的时区偏移。
-
Date.prototype.getUTCDate() - Returns the day (date) of the month (1-31) in the specified date according to universal time.
-
Date.prototype.getUTCDay() - Returns the day of the week (0-6) in the specified date according to universal time.
-
Date.prototype.getUTCFullYear() - Returns the year (4 digits for 4-digit years) in the specified date according to universal time.
-
Date.prototype.getUTCHours() - Returns the hours (0-23) in the specified date according to universal time.
-
Date.prototype.getUTCMilliseconds() - Returns the milliseconds (0-999) in the specified date according to universal time.
-
Date.prototype.getUTCMinutes() - Returns the minutes (0-59) in the specified date according to universal time.
-
Date.prototype.getUTCMonth() - Returns the month (0-11) in the specified date according to universal time.
-
Date.prototype.getUTCSeconds() - Returns the seconds (0-59) in the specified date according to universal time.
-
Date.prototype.getYear() -
Returns the year (usually 2-3 digits) in the specified date according to local time. Use
getFullYear()instead.
Setter
-
Date.prototype.setDate() - 根据本地时间为指定的日期对象设置月份中的第几天。
-
Date.prototype.setFullYear() - 根据本地时间为指定日期对象设置完整年份(四位数年份是四个数字)。
-
Date.prototype.setHours() - 根据本地时间为指定日期对象设置小时数。
-
Date.prototype.setMilliseconds() - 根据本地时间为指定日期对象设置毫秒数。
-
Date.prototype.setMinutes() - 根据本地时间为指定日期对象设置分钟数。
-
Date.prototype.setMonth() - 根据本地时间为指定日期对象设置月份。
-
Date.prototype.setSeconds() - 根据本地时间为指定日期对象设置秒数。
-
Date.prototype.setTime() - 通过指定从 1970-1-1 00:00:00 UTC 开始经过的毫秒数来设置日期对象的时间,对于早于 1970-1-1 00:00:00 UTC的时间可使用负值。
-
Date.prototype.setUTCDate() - Sets the day of the month for a specified date according to universal time.
-
Date.prototype.setUTCFullYear() - Sets the full year (e.g. 4 digits for 4-digit years) for a specified date according to universal time.
-
Date.prototype.setUTCHours() - Sets the hour for a specified date according to universal time.
-
Date.prototype.setUTCMilliseconds() - Sets the milliseconds for a specified date according to universal time.
-
Date.prototype.setUTCMinutes() - Sets the minutes for a specified date according to universal time.
-
Date.prototype.setUTCMonth() - Sets the month for a specified date according to universal time.
-
Date.prototype.setUTCSeconds() - Sets the seconds for a specified date according to universal time.
-
Date.prototype.setYear() -
Sets the year (usually 2-3 digits) for a specified date according to local time. Use
setFullYear()instead.
Conversion getter
-
Date.prototype.toDateString() - 以人类易读(human-readable)的形式返回该日期对象日期部分的字符串。
-
Date.prototype.toISOString() - 把一个日期转换为符合 ISO 8601 扩展格式的字符串。
-
Date.prototype.toJSON() -
使用
toISOString()返回一个表示该日期的字符串。为了在JSON.stringify()方法中使用。 -
Date.prototype.toGMTString() -
返回一个基于 GMT (UT) 时区的字符串来表示该日期。请使用
toUTCString()方法代替。 -
Date.prototype.toLocaleDateString() - 返回一个表示该日期对象日期部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)。
-
Date.prototype.toLocaleFormat() - Converts a date to a string, using a format string.
-
Date.prototype.toLocaleString() -
返回一个表示该日期对象的字符串,该字符串与系统设置的地区关联(locality sensitive)。覆盖了
Object.prototype.toLocaleString()方法。 -
Date.prototype.toLocaleTimeString() - 返回一个表示该日期对象时间部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)。
-
Date.prototype.toSource() -
Returns a string representing the source for an equivalent
Dateobject; you can use this value to create a new object. Overrides theObject.prototype.toSource()method. -
Date.prototype.toString() -
返回一个表示该日期对象的字符串。覆盖了
Object.prototype.toString()方法。 -
Date.prototype.toTimeString() - 以人类易读格式返回日期对象时间部分的字符串。
-
Date.prototype.toUTCString() - 把一个日期对象转换为一个以UTC时区计时的字符串。
-
Date.prototype.valueOf() -
返回一个日期对象的原始值。覆盖了
Object.prototype.valueOf()方法。
例子
例子:创建一个日期对象的几种方法
下例展示了用来创建一个日期对象的多种方法。
var today = new Date();
var birthday = new Date("December 17, 1995 03:24:00");
var birthday = new Date("1995-12-17T03:24:00");
var birthday = new Date(1995,11,17);
var birthday = new Date(1995,11,17,3,24,0);
例子:两位数年份表示 1900 - 1999 年
为了能够创建和获取 0 到 99 之间的年份,应该使用 Date.prototype.setFullYear() 和 Date.prototype.getFullYear() 方法。
var date = new Date(98, 1); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT) // Deprecated method, 98 maps to 1998 here as well date.setYear(98); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT) date.setFullYear(98); // Sat Feb 01 0098 00:00:00 GMT+0000 (BST)
例子:计算经过的时间
下例展示了如何计算两个日期对象的时间差:
// using Date objects var start = Date.now(); // the event to time goes here: doSomethingForALongTime(); var end = Date.now(); var elapsed = end - start; // elapsed time in milliseconds
// using built-in methods var start = new Date(); // the event to time goes here: doSomethingForALongTime(); var end = new Date(); var elapsed = end.getTime() - start.getTime(); // elapsed time in milliseconds
// to test a function and get back its return
function printElapsedTime (fTest) {
var nStartTime = Date.now(),
vReturn = fTest(),
nEndTime = Date.now();
alert("Elapsed time: " + String(nEndTime - nStartTime) + " milliseconds");
return vReturn;
}
yourFunctionReturn = printElapsedTime(yourFunction);
注意:在支持 Web Performance API 的高精细度(high-resolution)时间功能的浏览器中,window.performance.now 提供的所经过的时间比 Date.now 更可靠和精确。
规范
| 规范版本 | 规范状态 | 注解 |
|---|---|---|
| ECMAScript 1st Edition. Implemented in JavaScript 1.1 | Standard | Initial definition. |
| ECMAScript 5.1 (ECMA-262) Date |
Standard | |
| ECMAScript 6 (ECMA-262) Date |
Draft | |
浏览器兼容性
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |