From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/date/@@toprimitive/index.html | 61 +++++ .../reference/global_objects/date/date/index.html | 108 +++++++++ .../global_objects/date/getdate/index.html | 78 +++++++ .../global_objects/date/getday/index.html | 88 ++++++++ .../global_objects/date/getfullyear/index.html | 89 ++++++++ .../global_objects/date/gethours/index.html | 75 +++++++ .../global_objects/date/getmilliseconds/index.html | 76 +++++++ .../global_objects/date/getminutes/index.html | 73 ++++++ .../global_objects/date/getmonth/index.html | 89 ++++++++ .../global_objects/date/getseconds/index.html | 77 +++++++ .../global_objects/date/gettime/index.html | 97 ++++++++ .../date/gettimezoneoffset/index.html | 65 ++++++ .../global_objects/date/getutcdate/index.html | 75 +++++++ .../global_objects/date/getutcday/index.html | 77 +++++++ .../global_objects/date/getutcfullyear/index.html | 76 +++++++ .../global_objects/date/getutchours/index.html | 76 +++++++ .../date/getutcmilliseconds/index.html | 74 ++++++ .../global_objects/date/getutcminutes/index.html | 74 ++++++ .../global_objects/date/getutcmonth/index.html | 76 +++++++ .../global_objects/date/getutcseconds/index.html | 74 ++++++ .../global_objects/date/getyear/index.html | 114 ++++++++++ .../reference/global_objects/date/index.html | 248 +++++++++++++++++++++ .../reference/global_objects/date/now/index.html | 101 +++++++++ .../reference/global_objects/date/parse/index.html | 184 +++++++++++++++ .../global_objects/date/prototype/index.html | 180 +++++++++++++++ .../global_objects/date/setdate/index.html | 79 +++++++ .../global_objects/date/setfullyear/index.html | 81 +++++++ .../global_objects/date/sethours/index.html | 88 ++++++++ .../global_objects/date/setmilliseconds/index.html | 75 +++++++ .../global_objects/date/setminutes/index.html | 91 ++++++++ .../global_objects/date/setmonth/index.html | 86 +++++++ .../global_objects/date/setseconds/index.html | 83 +++++++ .../global_objects/date/settime/index.html | 80 +++++++ .../global_objects/date/setutcdate/index.html | 77 +++++++ .../global_objects/date/setutcfullyear/index.html | 94 ++++++++ .../global_objects/date/setutchours/index.html | 94 ++++++++ .../date/setutcmilliseconds/index.html | 84 +++++++ .../global_objects/date/setutcminutes/index.html | 92 ++++++++ .../global_objects/date/setutcmonth/index.html | 90 ++++++++ .../global_objects/date/setutcseconds/index.html | 92 ++++++++ .../global_objects/date/setyear/index.html | 72 ++++++ .../global_objects/date/todatestring/index.html | 72 ++++++ .../global_objects/date/togmtstring/index.html | 67 ++++++ .../global_objects/date/toisostring/index.html | 89 ++++++++ .../global_objects/date/tojson/index.html | 73 ++++++ .../date/tolocaledatestring/index.html | 155 +++++++++++++ .../global_objects/date/tolocaleformat/index.html | 73 ++++++ .../global_objects/date/tolocalestring/index.html | 161 +++++++++++++ .../date/tolocaletimestring/index.html | 151 +++++++++++++ .../global_objects/date/tosource/index.html | 50 +++++ .../global_objects/date/tostring/index.html | 90 ++++++++ .../global_objects/date/totimestring/index.html | 73 ++++++ .../global_objects/date/toutcstring/index.html | 76 +++++++ .../reference/global_objects/date/utc/index.html | 104 +++++++++ .../global_objects/date/valueof/index.html | 86 +++++++ 55 files changed, 5083 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/@@toprimitive/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/date/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getdate/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getfullyear/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/gethours/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getmilliseconds/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getminutes/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getmonth/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getseconds/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/gettimezoneoffset/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getutcdate/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getutcday/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getutcfullyear/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getutchours/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getutcmilliseconds/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getutcminutes/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getutcmonth/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getutcseconds/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/now/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/prototype/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setdate/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setfullyear/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/sethours/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setmilliseconds/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setminutes/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setmonth/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setseconds/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/settime/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setutcdate/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setutcfullyear/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setutchours/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setutcmilliseconds/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setutcminutes/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setutcmonth/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setutcseconds/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/setyear/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/todatestring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/togmtstring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/toisostring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/tojson/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/tolocaleformat/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/tolocalestring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/tolocaletimestring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/tosource/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/tostring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/totimestring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/toutcstring/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/utc/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/valueof/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/date') diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/@@toprimitive/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/@@toprimitive/index.html new file mode 100644 index 0000000000..5bf2e177ee --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/@@toprimitive/index.html @@ -0,0 +1,61 @@ +--- +title: 'Date.prototype[@@toPrimitive]' +slug: Web/JavaScript/Reference/Global_Objects/Date/@@toPrimitive +translation_of: Web/JavaScript/Reference/Global_Objects/Date/@@toPrimitive +--- +
{{JSRef}}
+ +

[@@toPrimitive]() 方法可以转换一个 Date 对象到一个原始值。

+ +

用法

+ +
Date()[Symbol.toPrimitive](hint);
+
+ +

返回值

+ +

给出的 {{jsxref("Date")}} 的原始值。根据传入参数的不同,可以返回 string 或 number 类型。

+ +

说明

+ +

{{jsxref("Date")}} 对象的 [@@toPrimitive]() 方法可以返回一个原始值,或是 string,或是number。

+ +

如果 hint"string""default"[@@toPrimitive]() 将会调用 {{jsxref("Object.prototype.toString()", "toString")}}。如果 toString 属性不存在,则调用 {{jsxref("Object.prototype.valueOf()", "valueOf")}}。如果 valueOf 也不存在,则抛出一个{{jsxref("TypeError")}}。

+ +

如果 hint"number"[@@toPrimitive]() 会首先尝试 valueOf,若失败再尝试 toString

+ +

当期望一个原始值却收到一个对象时,JavaScript 可以自动的调用 [@@toPrimitive]() 方法来将一个对象转化成原始值,所以你很少会需要自己调用这个方法。

+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-date.prototype-@@toprimitive', 'Date.prototype.@@toPrimitive')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-date.prototype-@@toprimitive', 'Date.prototype.@@toPrimitive')}}{{Spec2('ESDraft')}} 
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Date.@@toPrimitive")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/date/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/date/index.html new file mode 100644 index 0000000000..47f7d4bd7d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/date/index.html @@ -0,0 +1,108 @@ +--- +title: Date() constructor +slug: Web/JavaScript/Reference/Global_Objects/Date/Date +translation_of: Web/JavaScript/Reference/Global_Objects/Date/Date +--- +
{{JSRef}}
+ +

Creates a JavaScript Date instance that represents a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.

+ +
{{EmbedInteractiveExample("pages/js/date-constructor.html")}}
+ + + +

Syntax

+ +
new Date()
+new Date(value)
+new Date(dateString)
+new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
+
+ +
+

Note: The only correct way to instantiate a new Date object is by using the {{jsxref("new")}} operator. If you simply call the Date object directly, such as now = Date(), the returned value is a string rather than a Date object.

+
+ +

Parameters

+ +

There are four basic forms for the Date() constructor:

+ +
    +
  1. +

    No parameters

    + +

    When no parameters are provided, the newly-created Date object represents the current date and time as of the time of instantiation.

    +
  2. +
  3. +

    Time value or timestamp number

    + +
    +
    value
    +
    An integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC (the ECMAScript epoch, equivalent to the UNIX epoch), with leap seconds ignored. Keep in mind that most UNIX Timestamp functions are only accurate to the nearest second.
    +
    +
  4. +
  5. +

    Timestamp string

    + +
    +
    dateString
    +
    A string value representing a date, specified in a format recognized by the {{jsxref("Date.parse()")}} method. (These formats are IETF-compliant RFC 2822 timestamps, and also strings in a version of ISO8601.) +
    +

    Note: Parsing of date strings with the Date constructor (and Date.parse(), which works the same way) is strongly discouraged due to browser differences and inconsistencies.

    + +
      +
    • Support for RFC 2822 format strings is by convention only.
    • +
    • Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.
    • +
    +
    +
    +
    +
  6. +
  7. +

    Individual date and time component values

    + +

    Given at least a year and month, this form of Date() returns a Date object whose component values (year, month, day, hour, minute, second, and millisecond) all come from the following parameters. Any missing fields are given the lowest possible value (1 for day and 0 for every other component).

    + +
    +
    year
    +
    +

    Integer value representing the year.

    + +

    Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year. See the example below.

    +
    +
    monthIndex
    +
    Integer value representing the month, beginning with 0 for January to 11 for December.
    +
    day {{optional_inline}}
    +
    Integer value representing the day of the month. The default is 1.
    +
    hours {{optional_inline}}
    +
    Integer value representing the hour of the day. The default is 0 (midnight).
    +
    minutes {{optional_inline}}
    +
    Integer value representing the minute segment of a time. The default is 0 minutes past the hour.
    +
    seconds {{optional_inline}}
    +
    Integer value representing the second segment of a time. The default is 0 seconds past the minute.
    +
    milliseconds {{optional_inline}}
    +
    Integer value representing the millisecond segment of a time. The default is 0 milliseconds past the second.
    +
    +
  8. +
+ +

Specifications

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-date-constructor', 'Date')}}
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Date.Date")}}

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getdate/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getdate/index.html new file mode 100644 index 0000000000..2c27867870 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getdate/index.html @@ -0,0 +1,78 @@ +--- +title: Date.prototype.getDate() +slug: Web/JavaScript/Reference/Global_Objects/Date/getDate +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDate +--- +

{{JSRef("Global_Objects", "Date")}}

+ +

根据本地时间,返回一个指定的日期对象为一个月中的哪一日(从1--31)。

+ +
{{EmbedInteractiveExample("pages/js/date-getdate.html")}}
+ + + +

语法

+ +
dateObj.getDate()
+ +

参数

+ +

+ +

描述

+ +

getDate() 返回一个1 到 31的整数值。

+ +

例子

+ +

例子: 使用getDate()方法

+ +

下面第二条语句将值25赋给 day 变量,该值基于日期对象 Xmax95的值。

+ +
var Xmas95 = new Date("December 25, 1995 23:15:00");
+var day = Xmas95.getDate();
+
+alert(day); // 25
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.1StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.14', 'Date.prototype.getDate')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getdate', 'Date.prototype.getDate')}}{{Spec2('ES6')}} +

 

+
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getDate")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html new file mode 100644 index 0000000000..20147639c4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html @@ -0,0 +1,88 @@ +--- +title: Date.prototype.getDay() +slug: Web/JavaScript/Reference/Global_Objects/Date/getDay +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDay +--- +

{{JSRef}}

+ +

getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天。对于某个月中的第几天,参考{{jsxref("Date.prototype.getDate()")}}.

+ +
{{EmbedInteractiveExample("pages/js/date-getday.html")}}
+ + + +

语法

+ +
dateObj.getDay()
+
+ +

返回值

+ +

根据本地时间,返回一个0到6之间的整数值,代表星期几: 0 代表星期日, 1 代表星期一,2 代表星期二, 依次类推。

+ +

例子

+ +

使用getDay()

+ +

下面第二条语句,基于{{jsxref("Date")}}对象 Xmas95 的值,把 1 赋值给 weekday。也就是说1995年12月25日是星期一。

+ +
var Xmas95 = new Date("December 25, 1995 23:15:30");
+var weekday = Xmas95.getDay();
+
+console.log(weekday); // 1
+ +
+

注意:如果需要,可以使用{{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}与一个额外的options 参数,从而返回这天的全称(如"Monday").使用此方法,结果会更加国际化:

+ +
var options = { weekday: 'long'};
+console.log(new Intl.DateTimeFormat('en-US', options).format(Xmas95));
+// Monday
+console.log(new Intl.DateTimeFormat('de-DE', options).format(Xmas95));
+// Montag
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ESDraft', '#sec-date.prototype.getday', 'Date.prototype.getDay')}}  {{Spec2('ESDraft')}}  
{{SpecName('ES6', '#sec-date.prototype.getday', 'Date.prototype.getDay')}}    {{Spec2('ES6')}}
{{SpecName('ES5.1', '#sec-15.9.5.16', 'Date.prototype.getDay')}}        {{Spec2('ES5.1')}}
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.0.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getDay")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getfullyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getfullyear/index.html new file mode 100644 index 0000000000..10a0870fac --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getfullyear/index.html @@ -0,0 +1,89 @@ +--- +title: Date.prototype.getFullYear() +slug: Web/JavaScript/Reference/Global_Objects/Date/getFullYear +tags: + - Date + - JavaScript + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getFullYear +--- +

{{JSRef}}

+ +

getFullYear() 方法根据本地时间返回指定日期的年份。

+ +

此方法替代 {{jsxref("Date.prototype.getYear()", "getYear()")}} 。

+ +
{{EmbedInteractiveExample("pages/js/date-getfullyear.html")}}
+ + + +

语法

+ +
dateObj.getFullYear()
+
+ +

返回值

+ +

根据当地时间,返回一个对应于给定日期的年份数字。

+ +

描述

+ +

getFullYear()返回的值是绝对数。 对于1000到9999之间的日期,getFullYear()返回一个四位数字,如1995。使用此函数确保在2000年后兼容。

+ +

例子

+ +

使用getFullYear()

+ +

下面的例子将当前年份的四位数值分配给变量year

+ +
var today = new Date();
+var year = today.getFullYear();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.10', 'Date.prototype.getFullYear')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getfullyear', 'Date.prototype.getFullYear')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.getfullyear', 'Date.prototype.getFullYear')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getFullYear")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/gethours/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/gethours/index.html new file mode 100644 index 0000000000..9eb86dc04a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/gethours/index.html @@ -0,0 +1,75 @@ +--- +title: Date.prototype.getHours() +slug: Web/JavaScript/Reference/Global_Objects/Date/getHours +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getHours +--- +

{{JSRef("Global_Objects", "Date")}}

+ +

getHours() 方法根据本地时间,返回一个指定的日期对象的小时。

+ +
{{EmbedInteractiveExample("pages/js/date-gethours.html")}}
+ + + +

语法

+ +
dateObj.getHours()
+ +

参数

+ +

+ +

返回值

+ +

getHours返回一个0 到 23之间的整数值。

+ +

例子

+ +

例子:使用getHours方法

+ +

下面第二条语句,基于日期对象 Xmas95 的值,把 23 赋值给了变量 hours。

+ +
var Xmas95 = new Date("December 25, 1995 23:15:00");
+var hours = Xmas95.getHours();
+
+alert(hours); // 23
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.18', 'Date.prototype.getHours')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.gethours', 'Date.prototype.getHours')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getHours")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getmilliseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getmilliseconds/index.html new file mode 100644 index 0000000000..5c2b81b6e2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getmilliseconds/index.html @@ -0,0 +1,76 @@ +--- +title: Date.prototype.getMilliseconds() +slug: Web/JavaScript/Reference/Global_Objects/Date/getMilliseconds +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getMilliseconds +--- +

{{JSRef("Global_Objects", "Date")}}

+ +

getMilliseconds() 方法,根据本地时间,返回一个指定的日期对象的毫秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-getmilliseconds.html")}}
+ + + +

语法

+ +
dateObj.getMilliseconds()
+ +

参数

+ +

+ +

描述

+ +

getMilliseconds() 方法返回一个0 到 999的整数。

+ +

例子

+ +

例子:使用getMilliseconds方法

+ +

下例中,将当前时间的毫秒数赋值给变量 ms

+ +
var ms;
+Today = new Date();
+ms = Today.getMilliseconds();
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.3StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.24', 'Date.prototype.getMilliseconds')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getmilliseconds', 'Date.prototype.getMilliseconds')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getMilliseconds")}}

+ +

相关链接

+ +

 

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getminutes/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getminutes/index.html new file mode 100644 index 0000000000..74ace83ef7 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getminutes/index.html @@ -0,0 +1,73 @@ +--- +title: Date.prototype.getMinutes() +slug: Web/JavaScript/Reference/Global_Objects/Date/getMinutes +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getMinutes +--- +

{{JSRef("Global_Objects", "Date")}}

+ +

getMinutes() 方法根据本地时间,返回一个指定的日期对象的分钟数。

+ +
{{EmbedInteractiveExample("pages/js/date-getminutes.html")}}
+ +

语法

+ +
dateObj.getMinutes()
+ +

参数

+ +

+ +

描述

+ +

getMinutes 返回一个0 到 59的整数值。

+ +

例子

+ +

例子:使用getMinutes方法

+ +

下例中,第二行语句运行过后,变量 minutes 的值为15,也就是说 Xmas95 这个日期对象的值为某时15分某秒。

+ +
var Xmas95 = new Date("December 25, 1995 23:15:00");
+var minutes = Xmas95.getMinutes();
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.20', 'Date.prototype.getMinutes')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getminutes', 'Date.prototype.getMinutes')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +

 

+ + + +

{{Compat("javascript.builtins.Date.getMinutes")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getmonth/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getmonth/index.html new file mode 100644 index 0000000000..bf2d68f24d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getmonth/index.html @@ -0,0 +1,89 @@ +--- +title: Date.prototype.getMonth() +slug: Web/JavaScript/Reference/Global_Objects/Date/getMonth +tags: + - Date + - JavaScript + - Prototype + - 原型 + - 参考 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getMonth +--- +
{{JSRef}}
+ +

根据本地时间,返回一个指定的日期对象的月份,为基于0的值(0表示一年中的第一月)。

+ +
{{EmbedInteractiveExample("pages/js/date-getmonth.html")}}
+ + + +

语法

+ +
dateObj.getMonth()
+ +

参数

+ +

+ +

返回值

+ +

getMonth返回一个0 到 11的整数值: 0 代表一月份,1 代表二月份, 2 代表三月份,依次类推。

+ +

例子

+ +

使用 getMonth()

+ +

下面第二条语句,基于 {{jsxref("Date")}} 对象 Xmas95 的值,把11赋值给变量 month。

+ +
var Xmas95 = new Date('December 25, 1995 23:15:30');
+var month = Xmas95.getMonth();
+
+console.log(month); // 11
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition.
{{SpecName('ES5.1', '#sec-15.9.5.12', 'Date.prototype.getMonth')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getmonth', 'Date.prototype.getMonth')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.getmonth', 'Date.prototype.getMonth')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getMonth")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getseconds/index.html new file mode 100644 index 0000000000..4edaa5e02c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getseconds/index.html @@ -0,0 +1,77 @@ +--- +title: Date.prototype.getSeconds() +slug: Web/JavaScript/Reference/Global_Objects/Date/getSeconds +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getSeconds +--- +

{{JSRef("Global_Objects", "Date")}}

+ +

getSeconds() 方法根据本地时间,返回一个指定的日期对象的秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-getseconds.html")}}
+ + + +

语法

+ +
dateObj.getSeconds()
+ +

参数

+ +

+ +

描述

+ +

该方法返回一个 0 到 59 的整数值。

+ +

例子

+ +

例子: 使用getSeconds方法

+ +

下面第二条语句,基于日期对象 Xmas95 的值,把 30 赋值给变量 secs

+ +
var Xmas95 = new Date("December 25, 1995 23:15:30");
+var secs = Xmas95.getSeconds();
+ +
 
+ +
 
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.22', 'Date.prototype.getSeconds')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getseconds', 'Date.prototype.getSeconds')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getSeconds")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html new file mode 100644 index 0000000000..3bac439efa --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/gettime/index.html @@ -0,0 +1,97 @@ +--- +title: Date.prototype.getTime() +slug: Web/JavaScript/Reference/Global_Objects/Date/getTime +tags: + - Date + - JavaScript + - 原型 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getTime +--- +
{{JSRef}}
+ +

getTime() 方法返回一个时间的格林威治时间数值。

+ +

你可以使用这个方法把一个日期时间赋值给另一个{{jsxref("Date")}} 对象。这个方法的功能和 {{jsxref("Date.valueof", "valueOf()")}} 方法一样。

+ +
{{EmbedInteractiveExample("pages/js/date-gettime.html")}}
+ + + +

语法

+ +
dateObj.getTime() 
+ +

参数

+ +

无。

+ +

返回值

+ +

getTime 方法的返回值一个数值,表示从1970年1月1日0时0分0秒(UTC,即协调世界时)距离该日期对象所代表时间的毫秒数。

+ +

例子

+ +

使用 getTime() 复制日期对象

+ +

创建一个拥有相同时间值的日期对象。

+ +
var birthday = new Date(1991, 9, 17);
+var copy = new Date();
+copy.setTime(birthday.getTime());
+
+ +

测量代码执行时间

+ +

连续调用两个新生成的日期对象的 getTime 方法,根据两次调用的返回值求得时间差。这可以用于计算某些操作的执行时间。避免生成不必要的{{jsxref("Date")}}对象另见{{jsxref("Date.now()")}} 

+ +
var end, start, i;
+
+start = new Date();
+for (i = 0; i < 1000; i++) {
+  Math.sqrt(i);
+}
+end = new Date();
+
+console.log("Operation took " + (end.getTime() - start.getTime()) + " msec");
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态说明
{{SpecName('ES1')}}{{Spec2('ES1')}}初始定义
{{SpecName('ES5.1', '#sec-15.9.5.9', 'Date.prototype.getTime')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.gettime', 'Date.prototype.getTime')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getTime")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/gettimezoneoffset/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/gettimezoneoffset/index.html new file mode 100644 index 0000000000..3f7ed584aa --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/gettimezoneoffset/index.html @@ -0,0 +1,65 @@ +--- +title: Date.prototype.getTimezoneOffset() +slug: Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getTimezoneOffset() 方法返回协调世界时(UTC)相对于当前时区的时间差值,单位为分钟。

+ +
{{EmbedInteractiveExample("pages/js/date-gettimezoneoffset.html")}}
+ + + +

语法

+ +
dateObj.getTimezoneOffset()
+ +

参数

+ +

+ +

返回值

+ +

时区偏差(time-zone offset)表示协调世界时(UTC)与本地时区之间的差值,单位为分钟。需要注意的是如果本地时区先于协调世界时,则该差值为正值,如果后于协调世界时则为负值。例如你所在时区为 UTC+10(澳大利亚东部标准时间),将会返回 -600。对于同一个时区,夏令时(Daylight Saving Time)将会改变这个值。

+ +

例子

+ +

例子: 使用getTimezoneOffset方法

+ +
var x = new Date();
+var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.26', 'Date.prototype.getTimezoneOffset')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-date.prototype.gettimezoneoffset', 'Date.prototype.getTimezoneOffset')}}{{Spec2('ES6')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getTimezoneOffset")}}

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getutcdate/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getutcdate/index.html new file mode 100644 index 0000000000..c39ec91325 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getutcdate/index.html @@ -0,0 +1,75 @@ +--- +title: Date.prototype.getUTCDate() +slug: Web/JavaScript/Reference/Global_Objects/Date/getUTCDate +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getUTCDate +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getUTCDate() 方法以世界时为标准,返回一个指定的日期对象为一个月中的第几天

+ +
{{EmbedInteractiveExample("pages/js/date-getutcdate.html")}}
+ +

语法

+ +
dateObj.getUTCDate()
+ +

参数

+ +

+ +

返回值

+ +

getUTCDate() 返回一个 1 到 31 的整数值

+ +

例子

+ +

例子: 使用 getUTCDate() 方法

+ +

下面的例子是把当前日期的天数部分赋值给变量 day.

+ +
var today = new Date();
+var day = today.getUTCDate();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.15', 'Date.prototype.getUTCDate')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getutcdate', 'Date.prototype.getUTCDate')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getUTCDate")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getutcday/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getutcday/index.html new file mode 100644 index 0000000000..82acb18189 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getutcday/index.html @@ -0,0 +1,77 @@ +--- +title: Date.prototype.getUTCDay() +slug: Web/JavaScript/Reference/Global_Objects/Date/getUTCDay +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getUTCDay +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getUTCDay() 方法以世界时为标准,返回一个指定的日期对象为一星期中的第几天,其中 0 代表星期天。

+ +
{{EmbedInteractiveExample("pages/js/date-getutcday.html")}}
+ + + +

语法

+ +
dateObj.getUTCDay()
+ +

参数

+ +

无。

+ +

返回值

+ +

getUTCDay() 方法返回一个对应一星期中第几天的整数:0 代表星期天,1 代表星期一,2 代表星期二,依次类推。

+ +

例子

+ +

例子: 使用 getUTCDay() 方法

+ +

下面的例子是把当前日期的星期部分赋值给变量 weekday

+ +
var today = new Date();
+var weekday = today.getUTCDay();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.17', 'Date.prototype.getUTCDay')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getutcday', 'Date.prototype.getUTCDay')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getUTCDay")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getutcfullyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getutcfullyear/index.html new file mode 100644 index 0000000000..8f19fa4845 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getutcfullyear/index.html @@ -0,0 +1,76 @@ +--- +title: Date.prototype.getUTCFullYear() +slug: Web/JavaScript/Reference/Global_Objects/Date/getUTCFullYear +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getUTCFullYear +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getUTCFullYear() 以世界时为标准,返回一个指定的日期对象的年份。

+ +
{{EmbedInteractiveExample("pages/js/date-getutcfullyear.html")}}
+ + + +

语法

+ +
dateObj.getUTCFullYear()
+ +

参数

+ +

无。

+ +

返回值

+ +

getUTCFullYear() 返回一个绝对数值,符合 Year-2000 标准,例如 1995。

+ +

例子

+ +

例子: 使用 getUTCFullYear() 方法

+ +

下面的例子是把当前年份的四位数值复制给变量 year

+ +
var today = new Date();
+var year = today.getUTCFullYear();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.11', 'Date.prototype.getUTCFullYear')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getutcfullyear', 'Date.prototype.getUTCFullYear')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getUTCFullYear")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getutchours/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getutchours/index.html new file mode 100644 index 0000000000..85c105c875 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getutchours/index.html @@ -0,0 +1,76 @@ +--- +title: Date.prototype.getUTCHours() +slug: Web/JavaScript/Reference/Global_Objects/Date/getUTCHours +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getUTCHours +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getUTCHours() 方法以世界时为标准,返回一个指定的日期对象的小时数。

+ +
{{EmbedInteractiveExample("pages/js/date-getutchours.html")}}
+ + + +

语法

+ +
dateObj.getUTCHours()
+ +

参数

+ +

无。

+ +

返回值

+ +

getUTCHours() 返回一个 0 到 23 的整数。

+ +

例子

+ +

例子: 使用 getUTCHours() 方法

+ +

下例将当前时间的小时部分赋值给变量 hours

+ +
var today = new Date();
+var hours = today.getUTCHours();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.19', 'Date.prototype.getUTCHours')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getutchours', 'Date.prototype.getUTCHours')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getUTCHours")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getutcmilliseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getutcmilliseconds/index.html new file mode 100644 index 0000000000..a77a980836 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getutcmilliseconds/index.html @@ -0,0 +1,74 @@ +--- +title: Date.prototype.getUTCMilliseconds() +slug: Web/JavaScript/Reference/Global_Objects/Date/getUTCMilliseconds +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getUTCMilliseconds +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getUTCMilliseconds() 方法以世界时为标准,返回一个指定的日期对象的毫秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-getutcmilliseconds.html")}}
+ +

语法

+ +
dateObj.getUTCMilliseconds()
+ +

参数

+ +

无。

+ +

返回值

+ +

getUTCMilliseconds() 返回一个 0 到 999 的整数。

+ +

例子

+ +

例子: 使用 getUTCMilliseconds() 方法

+ +

下例将当前时间的毫秒部分赋值给变量 milliseconds

+ +
var today = new Date();
+var milliseconds = today.getUTCMilliseconds();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.25', 'Date.prototype.getUTCMilliseconds')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getutcmilliseconds', 'Date.prototype.getUTCMilliseconds')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getUTCMilliseconds")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getutcminutes/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getutcminutes/index.html new file mode 100644 index 0000000000..f4df8aff72 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getutcminutes/index.html @@ -0,0 +1,74 @@ +--- +title: Date.prototype.getUTCMinutes() +slug: Web/JavaScript/Reference/Global_Objects/Date/getUTCMinutes +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getUTCMinutes +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getUTCMinutes() 方法以世界时为标准,返回一个指定的日期对象的分钟数。

+ +
{{EmbedInteractiveExample("pages/js/date-getutcminutes.html")}}
+ +

语法

+ +
dateObj.getUTCMinutes()
+ +

参数

+ +

无。

+ +

返回值

+ +

getUTCMinutes() 返回一个 0 到 59 的整数。

+ +

例子

+ +

例子: 使用 getUTCMinutes() 方法

+ +

下例将当前时间的分钟部分赋值给变量 minutes

+ +
var today = new Date();
+var minutes = today.getUTCMinutes();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.21', 'Date.prototype.getUTCMinutes')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getutcminutes', 'Date.prototype.getUTCMinutes')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getUTCMinutes")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getutcmonth/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getutcmonth/index.html new file mode 100644 index 0000000000..015b513a72 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getutcmonth/index.html @@ -0,0 +1,76 @@ +--- +title: Date.prototype.getUTCMonth() +slug: Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getUTCMonth() 方法以世界时为标准,返回一个指定的日期对象的月份,它是从 0 开始计数的(0 代表一年的第一个月)。

+ +
{{EmbedInteractiveExample("pages/js/date-getutcmonth.html")}}
+ + + +

语法

+ +
dateObj.getUTCMonth()
+ +

参数

+ +

无。

+ +

返回值

+ +

getUTCMonth() 返回一个 0 到 11 的整数,分别对应以下月份:0 代表一月,1 代表二月,2 代表三月,依次类推。

+ +

例子

+ +

例子: 使用 getUTCMonth() 方法

+ +

下例将当前时间的月份赋值给变量 month

+ +
var today = new Date();
+var month = today.getUTCMonth();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.13', 'Date.prototype.getUTCMonth')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getutcmonth', 'Date.prototype.getUTCMonth')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getUTCMonth")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getutcseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getutcseconds/index.html new file mode 100644 index 0000000000..84f2e82ff3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getutcseconds/index.html @@ -0,0 +1,74 @@ +--- +title: Date.prototype.getUTCSeconds() +slug: Web/JavaScript/Reference/Global_Objects/Date/getUTCSeconds +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getUTCSeconds +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

getUTCSeconds() 方法以世界时为标准,返回一个指定的日期对象的秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-getutcseconds.html")}}
+ +

语法

+ +
dateObj.getUTCSeconds()
+ +

参数

+ +

无。

+ +

返回值

+ +

getUTCSeconds() 返回一个 0 到 59 的整数。

+ +

例子

+ +

例子: 使用 getUTCSeconds() 方法

+ +

下例将当前时间的秒数部分赋值给变量 seconds

+ +
var today = new Date();
+var seconds = today.getUTCSeconds();
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.23', 'Date.prototype.getUTCSeconds')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.getutcseconds', 'Date.prototype.getUTCSeconds')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.getUTCSeconds")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html new file mode 100644 index 0000000000..29f69b85d5 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getyear/index.html @@ -0,0 +1,114 @@ +--- +title: Date.prototype.getYear() +slug: Web/JavaScript/Reference/Global_Objects/Date/getYear +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getYear +--- +
{{JSRef("Global_Objects", "Date")}} {{Deprecated_header("")}}
+ +

getYear() 方法返回指定的本地日期的年份。因为 getYear 不返回千禧年[full years] ("year 2000 problem"),所以这个方法不再被使用,现在替换为 {{jsxref("Date.getFullYear", "getFullYear")}} .

+ +

Syntax

+ +
dateObj.getYear() 
+ +

Parameters

+ +

None.

+ +

Returns

+ +

The getYear method returns the year minus 1900; thus:

+ + + +

To take into account years before and after 2000, you should use {{jsxref("Date.getFullYear", "getFullYear()")}} instead of getYear so that the year is specified in full.

+ +

Backward Compatibility

+ +

Behaviour in JavaScript 1.2 and earlier

+ +

The getYear method returns either a 2-digit or 4-digit year:

+ + + +

Examples

+ +

Example: Years between 1900 and 1999

+ +

The second statement assigns the value 95 to the variable year.

+ +
var Xmas = new Date("December 25, 1995 23:15:00");
+var year = Xmas.getYear(); // returns 95
+
+ +

Example: Years above 1999

+ +

The second statement assigns the value 100 to the variable year.

+ +
var Xmas = new Date("December 25, 2000 23:15:00");
+var year = Xmas.getYear(); // returns 100
+
+ +

Example: Years below 1900

+ +

The second statement assigns the value -100 to the variable year.

+ +
var Xmas = new Date("December 25, 1800 23:15:00");
+var year = Xmas.getYear(); // returns -100
+
+ +

Example: Setting and getting a year between 1900 and 1999

+ +

The second statement assigns the value 95 to the variable year, representing the year 1995.

+ +
var Xmas.setYear(95);
+var year = Xmas.getYear(); // returns 95
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-B.2.4', 'Date.prototype.getYear')}}{{Spec2('ES5.1')}}Defined in the (informative) compatibility annex.
{{SpecName('ES6', '#sec-date.prototype.getyear', 'Date.prototype.getYear')}}{{Spec2('ES6')}}Defined in the (normative) annex for additional features for web browsers.
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Date.getYear")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/index.html new file mode 100644 index 0000000000..9cacee3238 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/index.html @@ -0,0 +1,248 @@ +--- +title: Date +slug: Web/JavaScript/Reference/Global_Objects/Date +tags: + - Date + - JavaScript + - 日期 + - 时间 +translation_of: Web/JavaScript/Reference/Global_Objects/Date +--- +

{{JSRef}}

+ +

创建一个 JavaScript Date 实例,该实例呈现时间中的某个时刻。Date 对象则基于 Unix Time Stamp,即自1970年1月1日(UTC)起经过的毫秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-constructor.html")}}
+ + + +

语法

+ +
new Date();
+new Date(value);
+new Date(dateString);
+new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
+ +
+

创建一个新Date对象的唯一方法是通过{{jsxref("Operators/new", "new")}} 操作符,例如:let now = new Date();
+ 若将它作为常规函数调用(即不加 {{jsxref("Operators/new", "new")}} 操作符),将返回一个字符串,而非 Date 对象。 

+
+ +

参数

+ +

Date()构造函数有四种基本形式

+ +

没有参数

+ +

如果没有提供参数,那么新创建的Date对象表示实例化时刻的日期和时间。

+ +

Unix时间戳

+ +
+
value
+
一个 Unix 时间戳(Unix Time Stamp),它是一个整数值,表示自1970年1月1日00:00:00 UTC(the Unix epoch)以来的毫秒数,忽略了闰秒。请注意大多数 Unix 时间戳功能仅精确到最接近的秒。
+
+

时间戳字符串

+
+
dateString
+
表示日期的字符串值。该字符串应该能被 {{jsxref("Date.parse()")}} 正确方法识别(即符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。 +
+

注意: 由于浏览器之间的差异与不一致性,强烈不推荐使用Date构造函数来解析日期字符串 (或使用与其等价的Date.parse)。对 RFC 2822 格式的日期仅有约定俗称的支持。 对 ISO 8601 格式的支持中,仅有日期的串 (例如 "1970-01-01") 会被处理为 UTC 而不是本地时间,与其他格式的串的处理不同。

+
+
+
+ +

分别提供日期与时间的每一个成员

+ +

当至少提供了年份与月份时,这一形式的 Date() 返回的 Date 对象中的每一个成员都来自下列参数。没有提供的成员将使用最小可能值(对日期为1,其他为0)。

+ +
+
year
+
表示年份的整数值。 0到99会被映射至1900年至1999年,其它值代表实际年份。参见 {{anch("Two_digit_years_map_to_1900_-_1999", "示例")}}。
+
monthIndex
+
表示月份的整数值,从 0(1月)到 11(12月)。
+
date{{optional_inline}}
+
表示一个月中的第几天的整数值,从1开始。默认值为1。
+
hours {{optional_inline}}
+
表示一天中的小时数的整数值 (24小时制)。默认值为0(午夜)。
+
minutes {{optional_inline}}
+
表示一个完整时间(如 01:10:00)中的分钟部分的整数值。默认值为0。
+
seconds {{optional_inline}}
+
表示一个完整时间(如 01:10:00)中的秒部分的整数值。默认值为0。
+
milliseconds {{optional_inline}}
+
表示一个完整时间的毫秒部分的整数值。默认值为0。
+
+ +

使用注释

+ +
+

注意 参数monthIndex 是从“0”开始计算的,这就意味着一月份为“0”,十二月份为“11”。

+
+ +
+

注意:当Date作为构造函数调用并传入多个参数时,如果数值大于合理范围时(如月份为 13 或者分钟数为 70),相邻的数值会被调整。比如 new Date(2013, 13, 1)等于new Date(2014, 1, 1),它们都表示日期2014-02-01(注意月份是从0开始的)。其他数值也是类似,new Date(2013, 2, 1, 0, 70)等于new Date(2013, 2, 1, 1, 10),都表示同一个时间:2013-03-01T01:10:00

+
+ +
+

注意:当Date作为构造函数调用并传入多个参数时,所定义参数代表的是当地时间。如果需要使用世界协调时 UTC,使用 new Date({{jsxref("Date.UTC()", "Date.UTC(...)")}}) 和相同参数。

+
+ +

简介

+ + + +

属性

+ +
+
{{jsxref("Date.prototype")}}
+
允许为 Date 对象添加属性。
+
Date.length
+
Date.length 的值是 7。这是该构造函数可接受的参数个数。
+
+ +

方法

+ +
+
{{jsxref("Date.now()")}}
+
返回自 1970-1-1 00:00:00  UTC(世界标准时间)至今所经过的毫秒数。
+
{{jsxref("Date.parse()")}}
+
解析一个表示日期的字符串,并返回从 1970-1-1 00:00:00 所经过的毫秒数。 +
+

注意: 由于浏览器差异和不一致,强烈建议不要使用Date.parse解析字符串。

+
+
+
{{jsxref("Date.UTC()")}}
+
接受和构造函数最长形式的参数相同的参数(从2到7),并返回从 1970-01-01 00:00:00 UTC 开始所经过的毫秒数。
+
+ +

JavaScript Date 实例

+ +

所有的 Date 实例都继承自 {{jsxref("Date.prototype")}}。修改 Date 构造函数的原型对象会影响到所有的 Date 实例。

+ +

实例属性

+ +
+
Date.prototype.constructor
+
返回创建了实例的构造函数,默认是 {{jsxref("Date")}} 构造函数。
+
+ +

实例方法

+ +

{{ page("/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/prototype", "Methods") }}

+ +

例子

+ +

>例子:创建一个日期对象的几种方法

+ +

下例展示了用来创建一个日期对象的多种方法。

+ +
+

注意: 由于浏览器差异和不一致性,强烈建议不要使用Date构造函数(和Date.parse,它们是等效的)解析日期字符串。

+
+ +
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 之间的年份,应使用 {{jsxref("Date.prototype.setFullYear()")}} 和 {{jsxref("Date.prototype.getFullYear()")}} 方法。

+ +
var date = new Date(98, 1); // Sun Feb 01 1998 00:00:00 GMT+0000 (GMT)
+
+// 已弃用的方法, 同样将 98 映射为 1998
+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)
+
+ +

例子:计算经过的时间

+ +

下例展示了如何以毫秒精度计算两个日期对象的时间差:

+ +

由于不同日期、月份、年份长度的不同(日期长度不同来自夏令时的切换),使用大于秒、分钟、小时的单位表示经过的时间会遇到很多问题,在使用前需要经过详尽的调研。

+ +
// 使用 Date 对象
+var start = Date.now();
+
+// 调用一个消耗一定时间的方法:
+doSomethingForALongTime();
+var end = Date.now();
+var elapsed = end - start; // 以毫秒计的运行时长
+ +
// 使用内建的创建方法
+var start = new Date();
+
+// 调用一个消耗一定时间的方法:
+doSomethingForALongTime();
+var end = new Date();
+var elapsed = end.getTime() - start.getTime(); // 运行时间的毫秒值
+ +
// 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);
+
+ +
+

注意:在支持 {{domxref("window.performance", "Web Performance API")}} 的高精细度(high-resolution)时间功能的浏览器中,{{domxref("Performance.now()")}} 提供的所经过的时间比 {{jsxref("Date.now()")}} 更加可靠、精确。

+
+ +

获取自 Unix 起始时间以来经过的秒数

+ +
var seconds = Math.floor(Date.now() / 1000);
+ +

注意此处需要返回一个整数 (仅做除法得到的不是整数),并且需要返回实际已经经过的秒数(所以这里使用了{{jsxref("Math.floor()")}}而不是{{jsxref("Math.round()")}}).

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ESDraft', '#sec-date-objects', 'Date')}}{{Spec2('ESDraft')}}
{{SpecName('ES6', '#sec-date-objects', 'Date')}}{{Spec2('ES6')}}
{{SpecName('ES5.1', '#sec-15.9', 'Date')}}{{Spec2('ES5.1')}}
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.1.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date")}}

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/now/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/now/index.html new file mode 100644 index 0000000000..317ecc8b69 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/now/index.html @@ -0,0 +1,101 @@ +--- +title: Date.now() +slug: Web/JavaScript/Reference/Global_Objects/Date/now +tags: + - Date + - JavaScript + - Method + - polyfill + - 参考 + - 方法 + - 时间 +translation_of: Web/JavaScript/Reference/Global_Objects/Date/now +--- +
{{JSRef}}
+ +

Date.now() 方法返回自 1970 年 1 月 1 日 00:00:00 (UTC) 到当前时间的毫秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-now.html")}}
+ +

语法

+ +
var timeInMs = Date.now();
+
+ +

返回值

+ +

一个 {{jsxref("Number")}},表示自 UNIX 纪元开始(1970 年 1 月 1 日 00:00:00 (UTC))到当前时间的毫秒数。

+ +

描述

+ +

因为 now() 是 {{jsxref("Date")}} 的一个静态函数,所以必须以 Date.now() 的形式来使用。

+ +

时间精度被降低

+ +

为了提供针对定时攻击和指纹追踪的保护,Date.now() 的精度可能会根据浏览器的高级设置项目而被取整。
+ 在 Firefox 中,默认启用 privacy.reduceTimerPrecision 设置项,在 Firefox 59 中,默认被取整至 20 微秒;在 Firefox 60 中,则被取整至 2 毫秒。

+ +
// reduced time precision (2ms) in Firefox 60
+Date.now()
+// 1519211809934
+// 1519211810362
+// 1519211811670
+// ...
+
+
+// reduced time precision with `privacy.resistFingerprinting` enabled
+Date.now();
+// 1519129853500
+// 1519129858900
+// 1519129864400
+// ...
+
+ +

在 Firefox 中,还可以通过启用 privacy.resistFingerprinting 来进一步降低精度。启用后,精度将为 100 毫秒或者 privacy.resistFingerprinting.reduceTimerPrecision.microseconds 的值,取决于这两个值中哪一个更大,也就是,精度更低一些。

+ +

Polyfill

+ +

该方法在 ECMA-262 第五版中被标准化,可以通过下面的代码端来兼容那些不支持该方法的引擎:

+ +
if (!Date.now) {
+  Date.now = function now() {
+    return new Date().getTime();
+  };
+}
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范版本规范状态说明
{{SpecName('ES5.1', '#sec-15.9.4.4', 'Date.now')}}{{Spec2('ES5.1')}}初始定义。在 JavaScript 1.5 中实现
{{SpecName('ES6', '#sec-date.now', 'Date.now')}}{{Spec2('ES6')}} 
+  
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.now")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html new file mode 100644 index 0000000000..701f6fc103 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/parse/index.html @@ -0,0 +1,184 @@ +--- +title: Date.parse() +slug: Web/JavaScript/Reference/Global_Objects/Date/parse +tags: + - Date + - JavaScript + - Method + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Date/parse +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

Date.parse() 方法解析一个表示某个日期的字符串,并返回从1970-1-1 00:00:00 UTC 到该日期对象(该日期对象的UTC时间)的毫秒数,如果该字符串无法识别,或者一些情况下,包含了不合法的日期数值(如:2015-02-31),则返回值为NaN。

+ +

不推荐在ES5之前使用Date.parse方法,因为字符串的解析完全取决于实现。直到至今,不同宿主在如何解析日期字符串上仍存在许多差异,因此最好还是手动解析日期字符串(在需要适应不同格式时库能起到很大帮助)。    

+ +
{{EmbedInteractiveExample("pages/js/date-parse.html")}}
+ + + +

语法

+ +

显式调用:

+ +
Date.parse(dateString)
+ +

隐式调用:

+ +
new Date(dateString).getTime()
+
+ +

参数

+ +
+
dateString
+
一个符合 RFC2822 或 ISO 8601 日期格式的字符串(其他格式也许也支持,但结果可能与预期不符)。
+
+ +

返回值

+ +
+
一个表示从1970-1-1 00:00:00 UTC到给定日期字符串所表示时间的毫秒数的数值。如果参数不能解析为一个有效的日期,则返回{{jsxref("NaN")}}。
+
+ +

描述

+ +

parse 方法接受一个日期字符串(例如 "Dec 25, 1995"),并返回从1970-1-1 00:00:00 UTC到该日期字符串所表示日期的毫秒数。该方法在基于字符串值设置日期值时很有用,例如结合使用{{jsxref("Global_Objects/Date/setTime", "setTime()")}} 方法和 {{jsxref("Global_Objects/Date", "Date()")}} 构造函数。

+ +

parse 方法接受一个表示时间的字符串,返回相应的时间值。该方法可以接受符合 RFC2822 / IETF 日期语法 (RFC2822 Section 3.3) 的字符串,如 "Mon, 25 Dec 1995 13:30:00 GMT"。该方法能够理解美国大陆时区的缩写,但是为了更通用,应该使用时区偏移,如 "Mon, 25 Dec 1995 13:30:00 +0430" (格林威治的子午线向东偏移4小时30分钟)。如果没有指定时区,默认使用本地时区。

+ +

GMT 和 UTC 被看作相等。 如果 RFC2822 Section 3.3 格式中不包含时区信息时,会以本地时区来解析日期字符串。

+ +

由于在解析日期字符串时存在偏差会导致结果不一致,因此推荐始终手动解析日期字符串,特别是不同的ECMAScript实现会把诸如“2015-10-12 12:00:00”的字符串解析为NaN,UTC或者本地时间。

+ +

ECMAScript 5 ISO-8601 日期格式支持

+ +

另外,日期时间字符串也可以使用 ISO 8601 格式。例如,"2011-10-10" (仅日期)或 "2011-10-10T14:48:00" (日期和时间)能够作为参数被传递和解析。 如果参数字符串只包含日期格式,那么将会使用UTC时区来解析该参数。而如果是ISO 8601 格式中规定的时间加日期的格式,则将会被作为本地时区处理。

+ +

虽然在日期字符串解析过程中会使用时区修饰符,但返回值总会是从由NaN表示的1970-1-1 00:00:00 UTC到该日期字符串所表示日期的毫秒数。

+ +

由于 parse() 是 {{jsxref("Date")}} 的一个静态方法 , 所以应该使用 Date.parse() 来调用,而不是作为 {{jsxref("Date")}} 的实例方法。

+ +

默认时区的区别

+ +

当输入为 "March 7, 2014" 时, parse()  将默认使用本地时区。但如果使用 ISO  格式如 "2014-03-07" ,则会被默认为 UTC (ES5 和 ECMAScript 2015) 时区。  因此除非系统本地时区为 UTC,由这些字符串解析出的 {{jsxref("Date")}}  对象可能会因为 ECMAScript  版本不同而代表不同的时间。这意味着两个看起来等效的字符串可能因为它们的格式不同而被转换成不同的值。

+ +

引擎相关的日期格式

+ +

ECMAScript  规范规定:如果一个字符串不符合标准格式,则函数可以使用任何由引擎决定的策略或解析算法。 Date.parse()  对于因包含有无效元素而无法识别的 ISO 格式字符串或者日期应该返回 {{jsxref("NaN")}} 。

+ +

但是, 在如 ECMA-262 规范中定义的情况,如果因为无效值而导致日期字符串不能被识别为 ISO 格式时,根据浏览器和给定的值不同,返回值可以是,也可以不是 {{jsxref("NaN")}} 。比如:

+ +
// 包含无效值的非 ISO 格式字符串
+new Date('23/25/2014');
+ +

在 Firefox 30 中会被识别为本地时区的2015年12月25日,而在 Safari 7 中则是无效日期。但是,如果字符串被识别为 ISO 格式并且包含无效值,则在所有遵循 ES5 或者更新标准的浏览器中都会返回 {{jsxref("NaN")}} 。

+ +
// 包含无效值的 ISO 格式字符串
+new Date('2014-25-23').toISOString();
+// 在所有遵循 ES5的浏览器中返回 "RangeError: invalid date"
+ +

SpiderMonkey 的引擎策略可以在 jsdate.cpp  中找到。字符串 "10 06 2014"  可以作为非 ISO 格式字符串使用自定义处理方式的例子。参见这篇关于解析如何进行的粗略纲要

+ +
new Date('10 06 2014');
+ +

将会被解析为本地时间 2014年10月6日,而不是6月10日。另一个例子

+ +
new Date('foo-bar 2014').toString();
+// 返回: "Invalid Date"
+
+Date.parse('foo-bar 2014');
+// 返回: NaN
+ +

例子

+ +

例子:使用 Date.parse()

+ +

如果 IPOdate 是一个已经存在的 {{jsxref("Date")}} 对象,则可以把其设置为本地时间 1995年8月9日。如下:

+ +
IPOdate.setTime(Date.parse('Aug 9, 1995'));
+ +

其他一些解析非标准格式日期的例子:

+ +
Date.parse("Aug 9, 1995");
+
+ +

在时区 GMT-0300 中返回 807937200000 ,在其他时区中返回另外的值,因为未指定时区并且不是 ISO 格式,所以默认使用本地时区。

+ +
Date.parse("Wed, 09 Aug 1995 00:00:00 GMT");
+
+ +

因为指定了时区 GMT (UTC),所以不管本地时区如何,总是返回 807926400000

+ +
Date.parse("Wed, 09 Aug 1995 00:00:00");
+
+ +

在时区 GMT-0300 中返回 807937200000 ,在其他时区中返回另外的值,因为没有时区标志并且不是 ISO 格式,所以作为本地时区处理。

+ +
Date.parse("Thu, 01 Jan 1970 00:00:00 GMT");
+
+ +

因为指定了时区 GMT (UTC),所以不管本地时区如何,总是返回 0

+ +
Date.parse("Thu, 01 Jan 1970 00:00:00");
+
+ +

在时区 GMT-0400 中返回 14400000,在其他时区中返回另外的值,因为未指定时区并且不是 ISO 格式,所以使用本地时区处理。

+ +
Date.parse("Thu, 01 Jan 1970 00:00:00 GMT-0400");
+
+ +

因为指定了时区 GMT (UTC),所以不管本地时区如何,总是返回 14400000

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.4.2', 'Date.parse')}}{{Spec2('ES5.1')}}ISO 8601 format added
{{SpecName('ES6', '#sec-date.parse', 'Date.parse')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.parse', 'Date.parse')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +

 

+ + + +

{{Compat("javascript.builtins.Date.parse")}}

+ +

兼容性提示

+ +

Firefox 49 {{geckoRelease(49)}} 修改了解析2位数年份的方式,从和 Internet Explorer 一致改为和 Google Chrome  浏览器一致。现在,2位数的年份小于等于 50  的将会被解析为21世纪的年份。比如, 04/16/17 ,在之前会被解析为 1917年4月16日,现在将被解析为 2017年4月16日。为了避免任何可能的同步问题或者有歧义的年份,推荐使用 ISO 8601 格式如 "2017-04-16" ({{bug(1265136)}})。

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/prototype/index.html new file mode 100644 index 0000000000..198a479453 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/prototype/index.html @@ -0,0 +1,180 @@ +--- +title: Date.prototype +slug: Web/JavaScript/Reference/Global_Objects/Date/prototype +tags: + - Date + - JavaScript + - Property +translation_of: Web/JavaScript/Reference/Global_Objects/Date +--- +
{{JSRef}}
+ +

Date.prototype 属性表示{{jsxref("Date")}}构造函数的原型。

+ +
{{js_property_attributes(0,0,1)}}
+ +

描述

+ +

{{jsxref("Date")}}实例继承自Date.prototype。可以通过修改构造函数的原型对象来影响 {{jsxref("Date")}}实例继承的属性和方法。

+ +

为了兼容千禧年计算(也即考虑到 2000 年),应该总是指定完整的年份,例如,使用 1998,而不是 98。为了方便以完整的格式指定年份, JavaScript 包含了相应的方法{{jsxref("Global_Objects/Date/getFullYear", "getFullYear()")}},{{jsxref("Global_Objects/Date/setFullYear", "setFullYear()")}}, {{jsxref("Global_Objects/Date/getUTCFullYear", "getUTCFullYear()")}} 和{{jsxref("Global_Objects/Date/setUTCFullYear", "setUTCFullYear()")}}。

+ +

从 ECMAScript 6 开始,Date.prototype本身就是一个普通的对象。不是{{jsxref("Date")}}的实例。

+ +

属性

+ +
+
Date.prototype.constructor
+
返回创建该实例的函数。默认是Date构造函数。
+
+ +

方法

+ +

Getter

+ +
+
{{jsxref("Date.prototype.getDate()")}}
+
根据本地时间返回指定日期对象的月份中的第几天(1-31)。
+
{{jsxref("Date.prototype.getDay()")}}
+
根据本地时间返回指定日期对象的星期中的第几天(0-6)。
+
{{jsxref("Date.prototype.getFullYear()")}}
+
根据本地时间返回指定日期对象的年份(四位数年份时返回四位数字)。
+
{{jsxref("Date.prototype.getHours()")}}
+
根据本地时间返回指定日期对象的小时(0-23)。
+
{{jsxref("Date.prototype.getMilliseconds()")}}
+
根据本地时间返回指定日期对象的毫秒(0-999)。
+
{{jsxref("Date.prototype.getMinutes()")}}
+
根据本地时间返回指定日期对象的分钟(0-59)。
+
{{jsxref("Date.prototype.getMonth()")}}
+
根据本地时间返回指定日期对象的月份(0-11)。
+
{{jsxref("Date.prototype.getSeconds()")}}
+
根据本地时间返回指定日期对象的秒数(0-59)。
+
{{jsxref("Date.prototype.getTime()")}}
+
返回从1970-1-1 00:00:00 UTC(协调世界时)到该日期经过的毫秒数,对于1970-1-1 00:00:00 UTC之前的时间返回负值。
+
{{jsxref("Date.prototype.getTimezoneOffset()")}}
+
返回当前时区的时区偏移。
+
{{jsxref("Date.prototype.getUTCDate()")}}
+
根据世界时返回特定日期对象一个月的第几天(1-31).
+
{{jsxref("Date.prototype.getUTCDay()")}}
+
根据世界时返回特定日期对象一个星期的第几天(0-6).
+
{{jsxref("Date.prototype.getUTCFullYear()")}}
+
根据世界时返回特定日期对象所在的年份(4位数).
+
{{jsxref("Date.prototype.getUTCHours()")}}
+
根据世界时返回特定日期对象当前的小时(0-23).
+
{{jsxref("Date.prototype.getUTCMilliseconds()")}}
+
根据世界时返回特定日期对象的毫秒数(0-999).
+
{{jsxref("Date.prototype.getUTCMinutes()")}}
+
根据世界时返回特定日期对象的分钟数(0-59).
+
{{jsxref("Date.prototype.getUTCMonth()")}}
+
根据世界时返回特定日期对象的月份(0-11).
+
{{jsxref("Date.prototype.getUTCSeconds()")}}
+
根据世界时返回特定日期对象的秒数(0-59).
+
{{jsxref("Date.prototype.getYear()")}}{{deprecated_inline}}
+
根据特定日期返回年份 (通常 2-3 位数). 使用 {{jsxref("Global_Objects/Date/getFullYear", "getFullYear()")}} .
+
+ +

Setter

+ +
+
{{jsxref("Date.prototype.setDate()")}}
+
根据本地时间为指定的日期对象设置月份中的第几天。
+
{{jsxref("Date.prototype.setFullYear()")}}
+
根据本地时间为指定日期对象设置完整年份(四位数年份是四个数字)。
+
{{jsxref("Date.prototype.setHours()")}}
+
根据本地时间为指定日期对象设置小时数。
+
{{jsxref("Date.prototype.setMilliseconds()")}}
+
根据本地时间为指定日期对象设置毫秒数。
+
{{jsxref("Date.prototype.setMinutes()")}}
+
根据本地时间为指定日期对象设置分钟数。
+
{{jsxref("Date.prototype.setMonth()")}}
+
根据本地时间为指定日期对象设置月份。
+
{{jsxref("Date.prototype.setSeconds()")}}
+
根据本地时间为指定日期对象设置秒数。
+
{{jsxref("Date.prototype.setTime()")}}
+
通过指定从 1970-1-1 00:00:00 UTC 开始经过的毫秒数来设置日期对象的时间,对于早于 1970-1-1 00:00:00 UTC的时间可使用负值。
+
{{jsxref("Date.prototype.setUTCDate()")}}
+
根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
+
{{jsxref("Date.prototype.setUTCFullYear()")}}
+
根据世界时设置 Date 对象中的年份(四位数字)。
+
{{jsxref("Date.prototype.setUTCHours()")}}
+
根据世界时设置 Date 对象中的小时 (0 ~ 23)。
+
{{jsxref("Date.prototype.setUTCMilliseconds()")}}
+
根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
+
{{jsxref("Date.prototype.setUTCMinutes()")}}
+
根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
+
{{jsxref("Date.prototype.setUTCMonth()")}}
+
根据世界时设置 Date 对象中的月份 (0 ~ 11)。
+
{{jsxref("Date.prototype.setUTCSeconds()")}}
+
根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
+
{{jsxref("Date.prototype.setYear()")}} {{deprecated_inline}}
+
setYear() 方法用于设置年份。请使用 {{jsxref("Global_Objects/Date/setFullYear", "setFullYear()")}} 方法代替。
+
+ +

Conversion getter

+ +
+
{{jsxref("Date.prototype.toDateString()")}}
+
以人类易读(human-readable)的形式返回该日期对象日期部分的字符串。
+
{{jsxref("Date.prototype.toISOString()")}}
+
把一个日期转换为符合 ISO 8601 扩展格式的字符串。
+
{{jsxref("Date.prototype.toJSON()")}}
+
使用 {{jsxref("Global_Objects/Date/toISOString", "toISOString()")}} 返回一个表示该日期的字符串。为了在 {{jsxref("JSON.stringify()")}} 方法中使用。
+
{{jsxref("Date.prototype.toGMTString()")}} {{deprecated_inline}}
+
返回一个基于 GMT (UT) 时区的字符串来表示该日期。请使用 {{jsxref("Global_Objects/Date/toUTCString", "toUTCString()")}} 方法代替。
+
{{jsxref("Date.prototype.toLocaleDateString()")}}
+
返回一个表示该日期对象日期部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)。
+
{{jsxref("Date.prototype.toLocaleFormat()")}} {{non-standard_inline}}
+
使用格式字符串将日期转换为字符串。
+
{{jsxref("Date.prototype.toLocaleString()")}}
+
返回一个表示该日期对象的字符串,该字符串与系统设置的地区关联(locality sensitive)。覆盖了 {{jsxref("Global_Objects/Object/toLocaleString", "Object.prototype.toLocaleString()")}} 方法。
+
{{jsxref("Date.prototype.toLocaleTimeString()")}}
+
返回一个表示该日期对象时间部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)。
+
{{jsxref("Date.prototype.toSource()")}}{{non-standard_inline}}
+
返回一个与{{jsxref("Date")}}等价的原始字符串对象,你可以使用这个值去生成一个新的对象。重写了 {{jsxref("Object.prototype.toSource()")}} 这个方法。
+
{{jsxref("Date.prototype.toString()")}}
+
返回一个表示该日期对象的字符串。覆盖了{{jsxref("Object.prototype.toString()")}} 方法。
+
{{jsxref("Date.prototype.toTimeString()")}}
+
以人类易读格式返回日期对象时间部分的字符串。
+
{{jsxref("Date.prototype.toUTCString()")}}
+
把一个日期对象转换为一个以UTC时区计时的字符串。
+
{{jsxref("Date.prototype.valueOf()")}}
+
返回一个日期对象的原始值。覆盖了 {{jsxref("Object.prototype.valueOf()")}} 方法。
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.1.
{{SpecName('ES5.1', '#sec-15.9.5', 'Date.prototype')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-properties-of-the-date-prototype-object', 'Date.prototype')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-properties-of-the-date-prototype-object', 'Date.prototype')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.prototype")}}

diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setdate/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setdate/index.html new file mode 100644 index 0000000000..e044a3b9f3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setdate/index.html @@ -0,0 +1,79 @@ +--- +title: Date.prototype.setDate() +slug: Web/JavaScript/Reference/Global_Objects/Date/setDate +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setDate +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setDate() 方法根据本地时间来指定一个日期对象的天数。

+ +
{{EmbedInteractiveExample("pages/js/date-setdate.html")}}
+ + + +

语法

+ +
dateObj.setDate(dayValue)
+ +

参数

+ +
+
dayValue
+
一个整数,表示该月的第几天。
+
+ +

描述

+ +

如果 dayValue 超出了月份的合理范围,setDate 将会相应地更新 Date 对象。

+ +

例如,如果为 dayValue 指定0,那么日期就会被设置为上个月的最后一天。

+ +

如果dayValue被设置为负数,日期会设置为上个月最后一天往前数这个负数绝对值天数后的日期。-1会设置为上月最后一天的前一天(译者注:例如当前为4月,如果setDate(-2),则为3月29日)

+ +

例子

+ +

例子:使用setDate方法

+ +
var theBigDay = new Date(1962, 6, 7); // 1962-07-07
+theBigDay.setDate(24);  // 1962-07-24
+theBigDay.setDate(32);  // 1962-08-01
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.36', 'Date.prototype.setDate')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-date.prototype.setdate', 'Date.prototype.setDate')}}{{Spec2('ES6')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setDate")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setfullyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setfullyear/index.html new file mode 100644 index 0000000000..7f6b70ac9a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setfullyear/index.html @@ -0,0 +1,81 @@ +--- +title: Date.prototype.setFullYear() +slug: Web/JavaScript/Reference/Global_Objects/Date/setFullYear +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setFullYear +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setFullYear() 方法根据本地时间为一个日期对象设置年份。

+ +
{{EmbedInteractiveExample("pages/js/date-setfullyear.html")}}
+ + + +

语法

+ +
dateObj.setFullYear(yearValue[, monthValue[, dayValue]])
+ +

参数

+ +
+
yearValue
+
指定年份的整数值,例如1995。
+
monthValue
+
一个0到11之间的整数值,表示从一月到十二月。
+
dayValue
+
一个1到31之间的整数值,表示月份中的第几天。如果你指定了 dayValue 参数,就必须同时指定 monthValue
+
+ +

描述

+ +

如果没有指定 monthValuedayValue 参数,将会使用 getMonthgetDate 方法的返回值。

+ +

如果有一个参数超出了合理的范围,setFullYear 方法会更新其他参数值,日期对象的日期值也会被相应更新。 例如,为 monthValue 指定 15, 则年份会加1,月份值会为3。

+ +

例子

+ +

例子:使用setFullYear方法

+ +
var theBigDay = new Date();
+theBigDay.setFullYear(1997);
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.3StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.40', 'Date.prototype.setFullYear')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setfullyear', 'Date.prototype.setFullYear')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setFullYear")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/sethours/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/sethours/index.html new file mode 100644 index 0000000000..f23dab9d13 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/sethours/index.html @@ -0,0 +1,88 @@ +--- +title: Date.prototype.setHours() +slug: Web/JavaScript/Reference/Global_Objects/Date/setHours +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setHours +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setHours() 方法根据本地时间为一个日期对象设置小时数,返回从1970-01-01 00:00:00 UTC 到更新后的 {{jsxref("Global_Objects/Date", "日期")}} 对象实例所表示时间的毫秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-sethours.html")}}
+ + + +

语法

+ +
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
+
+ +

JavaScript 1.3版本之前

+ +
dateObj.setHours(hoursValue) 
+ +

参数

+ +
+
hoursValue
+
一个 0 到 23 的整数,表示小时。
+
minutesValue
+
一个 0 到 59 的整数,表示分钟。
+
secondsValue
+
一个 0 到 59 的整数,表示秒数。如果指定了 secondsValue 参数,则必须同时指定 minutesValue 参数。
+
msValue
+
一个 0 到 999 的数字,表示微秒数,如果指定了 msValue 参数,则必须同时指定 minutesValue 和 secondsValue 参数。
+
+ +

描述

+ +

如果不指定 minutesValuesecondsValue 和 msValue 参数,则会使用{{jsxref("Date.getMinutes", "getMinutes()")}},{{jsxref("Date.getSeconds", "getSeconds()")}} 和{{jsxref("Date.getMilliseconds", "getMilliseconds()")}} 方法的返回值。

+ +

如果有一个参数超出了合理范围,setHours 会相应地更新日期对象中的日期信息。例如,如果为 secondsValue 指定了 100,则分钟会加 1,然后秒数使用 40。

+ +

例子

+ +

例子:使用setHours方法

+ +
var theBigDay = new Date();
+theBigDay.setHours(7);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.34', 'Date.prototype.setHours')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.sethours', 'Date.prototype.setHours')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setHours")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setmilliseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setmilliseconds/index.html new file mode 100644 index 0000000000..8eac30a518 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setmilliseconds/index.html @@ -0,0 +1,75 @@ +--- +title: Date.prototype.setMilliseconds() +slug: Web/JavaScript/Reference/Global_Objects/Date/setMilliseconds +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setMilliseconds +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setMilliseconds() 方法会根据本地时间设置一个日期对象的豪秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-setmilliseconds.html")}}
+ + + +

语法

+ +
dateObj.setMilliseconds(millisecondsValue)
+ +

参数

+ +
+
millisecondsValue
+
一个 0 到 999 的数字,表示豪秒数。
+
+ +

描述

+ +

如果指定的数字超出了合理范围,则日期对象的时间信息会被相应地更新。例如,如果指定了 1005,则秒数加 1,豪秒数为 5。

+ +

例子

+ +

例子:使用setMilliseconds

+ +
var theBigDay = new Date();
+theBigDay.setMilliseconds(100);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.3StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.28', 'Date.prototype.setMilliseconds')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setmilliseconds', 'Date.prototype.setMilliseconds')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setMilliseconds")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setminutes/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setminutes/index.html new file mode 100644 index 0000000000..13421b2817 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setminutes/index.html @@ -0,0 +1,91 @@ +--- +title: Date.prototype.setMinutes() +slug: Web/JavaScript/Reference/Global_Objects/Date/setMinutes +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setMinutes +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setMinutes() 方法根据本地时间为一个日期对象设置分钟数。

+ +
{{EmbedInteractiveExample("pages/js/date-setminutes.html")}}
+ + + +

语法

+ +
dateObj.setMinutes(minutesValue[, secondsValue[, msValue]])
+ +

 JavaScript 1.3之前版本

+ +
dateObj.setMinutes(minutesValue)
+ +

参数

+ +
+
minutesValue
+
一个 0 到 59 的整数,表示分钟数。
+
+ +
+
secondsValue
+
一个 0 到 59 的整数,表示秒数。如果指定了 secondsValue 参数,则必须同时指定 minutesValue 参数。
+
+ +
+
msValue
+
一个 0 到 999 的数字,表示微秒数,如果指定了 msValue 参数,则必须同时指定 minutesValuesecondsValue 参数。
+
+ +

描述

+ +

如果没有指定 secondsValuemsValue 参数,就会使用 {{jsxref("Date.getSeconds", "getSeconds()")}} 和 {{jsxref("Date.getMilliseconds", "getmilliseconds()")}} 方法的返回值。

+ +

如果有一个指定的参数超出了合理范围,setMinutes 会相应地更新日期对象中的时间信息。例如,为 secondsValue 指定 100,分钟数将会加 1,而秒数会为 40。

+ +

例子

+ +

例子:使用setMinutes方法

+ +
var theBigDay = new Date();
+theBigDay.setMinutes(45);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.32', 'Date.prototype.setMinutes')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setminutes', 'Date.prototype.setMinutes')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setMinutes")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setmonth/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setmonth/index.html new file mode 100644 index 0000000000..010cf5f428 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setmonth/index.html @@ -0,0 +1,86 @@ +--- +title: Date.prototype.setMonth() +slug: Web/JavaScript/Reference/Global_Objects/Date/setMonth +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setMonth +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setMonth() 方法根据本地时间为一个设置年份的日期对象设置月份。

+ +
{{EmbedInteractiveExample("pages/js/date-setmonth.html")}}
+ +

语法

+ +
dateObj.setMonth(monthValue[, dayValue])
+ +

JavaScript 1.3版本之前

+ +
dateObj.setMonth(monthValue)
+ +

参数

+ +
+
monthValue
+
介于 0 到 11 之间的整数(表示一月到十二月)。
+
+ +
+
dayValue
+
从 1 到 31 之间的整数,表示月份中的第几天。0为上个月最后一天
+
返回值!
+
基于 1 January 1970 00:00:00 UTC 开始计算的毫秒数
+
+ +

描述

+ +

如果不指定 dayValue 参数,就会使用 {{jsxref("Date.getDate", "getDate")}} 方法的返回值。

+ +

如果有一个指定的参数超出了合理范围,setMonth 会相应地更新日期对象中的日期信息。例如,为 monthValue 指定 15,则年份会加 1,月份将会使用 3。

+ +

例子

+ +

例子:使用setMonth方法

+ +
var theBigDay = new Date();
+theBigDay.setMonth(6);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.38', 'Date.prototype.setMonth')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-date.prototype.setmonth', 'Date.prototype.setMonth')}}{{Spec2('ES6')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setMonth")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setseconds/index.html new file mode 100644 index 0000000000..5ace67cce9 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setseconds/index.html @@ -0,0 +1,83 @@ +--- +title: Date.prototype.setSeconds() +slug: Web/JavaScript/Reference/Global_Objects/Date/setSeconds +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setSeconds +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setSeconds() 方法根据本地时间设置一个日期对象的秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-setseconds.html")}}
+ + + +

语法

+ +
dateObj.setSeconds(secondsValue[, msValue])
+ +

JavaScript 1.3之前版本

+ +
dateObj.setSeconds(secondsValue)
+ +

参数

+ +
+
secondsValue
+
一个 0 到 59 的整数。
+
msValue
+
一个 0 到 999 的数字,表示微秒数。
+
+ +

描述

+ +

如果没有指定 msValue 参数,就会使用 {{jsxref("Date.getMilliseconds", "getMilliseconds()")}} 方法的返回值。

+ +

如果一个参数超出了合理范围, setSeconds 方法会相应地更新日期对象的时间信息。例如,为 secondsValue 指定 100,则日期对象的分钟数会相应地加 1,秒数将会使用 40。

+ +

例子

+ +

例子:使用setSeconds方法

+ +
var theBigDay = new Date();
+theBigDay.setSeconds(30)
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.30', 'Date.prototype.setSeconds')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setseconds', 'Date.prototype.setSeconds')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setSeconds")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/settime/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/settime/index.html new file mode 100644 index 0000000000..85fe608f8e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/settime/index.html @@ -0,0 +1,80 @@ +--- +title: Date.prototype.setTime() +slug: Web/JavaScript/Reference/Global_Objects/Date/setTime +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setTime +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setTime() 方法以一个表示从1970-1-1 00:00:00 UTC计时的毫秒数为来为 Date 对象设置时间。

+ +
{{EmbedInteractiveExample("pages/js/date-settime.html")}}
+ + + +

语法

+ +
dateObj.setTime(timeValue)
+ +

参数

+ +
+
timeValue
+
一个整数,表示从1970-1-1 00:00:00 UTC开始计时的毫秒数。
+
+

返回值

+
+
UTC 1970年1月1日00:00:00与更新日期之间的毫秒数(实际上是自变量的值)。
+
+ +

描述

+ +

使用 setTime 方法用来把一个日期时间赋值给另一个 Date 对象。

+ +

例子

+ +

例子:使用setTime

+ +
theBigDay = new Date("July 1, 1999");
+sameAsBigDay = new Date();
+sameAsBigDay.setTime(theBigDay.getTime());
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.27', 'Date.prototype.setTime')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-date.prototype.settime', 'Date.prototype.setTime')}}{{Spec2('ES6')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setTime")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setutcdate/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setutcdate/index.html new file mode 100644 index 0000000000..8f0a425e96 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setutcdate/index.html @@ -0,0 +1,77 @@ +--- +title: Date.prototype.setUTCDate() +slug: Web/JavaScript/Reference/Global_Objects/Date/setUTCDate +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setUTCDate +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

setUTCDate() 方法就是根据全球时间设置特定date对象的日期。

+ +
{{EmbedInteractiveExample("pages/js/date-setutcdate.html")}}
+ + + +

语法

+ +
dateObj.setUTCDate(dayValue)
+ +

参数

+ +
+
dayValue
+
一个1-31的整形数字,用来指定日期。
+
+ +

描述

+ +

如果你指定的参数超出了范围,setUTCDate()会尝试更新对应的{{jsxref("Global_Objects/Date", "Date")}} 中的日期信息。例如,如果你使用了40来作为参数,但是{{jsxref("Global_Objects/Date", "Date")}} 中存储的月份为6月,那么日期将被改写为10且月份被增到7月。

+ +

示例

+ +

使用Using setUTCDate()

+ +
var theBigDay = new Date();
+theBigDay.setUTCDate(20);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.37', 'Date.prototype.setUTCDate')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setutcdate', 'Date.prototype.setUTCDate')}}{{Spec2('ES6')}} 
+ +

浏览器兼容

+ +
+ + +

{{Compat("javascript.builtins.Date.setUTCDate")}}

+
+ +

相关内容

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setutcfullyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setutcfullyear/index.html new file mode 100644 index 0000000000..cf971aa9c2 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setutcfullyear/index.html @@ -0,0 +1,94 @@ +--- +title: Date.prototype.setUTCFullYear() +slug: Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear +tags: + - Date + - JavaScript + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear +--- +
{{JSRef}}
+ +

setUTCFullYear() 方法根据世界标准时间为一个具体日期设置年份。

+ +
{{EmbedInteractiveExample("pages/js/date-setutcfullyear.html")}}
+ + + +

语法

+ +
dateObj.setUTCFullYear(yearValue[, monthValue[, dayValue]])
+ +

参数

+ +
+
yearValue
+
指定年份整数值,例如,1995
+
monthValue
+
可选。指定一个0-11之间的整数值,代表从一月到十二月。
+
dayValue
+
可选。指定一个1-31之间的整数值,代表月份中的第几天。如果你指定了dayValue参数,那么你必须指定monthValue参数。
+
+ +

描述

+ +

如果你没有指定具体的monthValue和dayValue,将会使用 getUTCMonth 和getUTCDate 方法的返回值。

+ +

如果你指定的参数超出了期待范围,setUTCFullYear()方法将会根据Date对象,更新其他参数和日期信息。例如,如果你将monthValue设定为15,年份会增加1,月份值则为为3。

+ +

例子

+ +

使用 setUTCFullYear()

+ +
var theBigDay = new Date();
+theBigDay.setUTCFullYear(1997);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ES1')}}{{Spec2('ES1')}}初始定义。 在 JavaScript 1.3实施。
{{SpecName('ES5.1', '#sec-15.9.5.41', 'Date.prototype.setUTCFullYear')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setutcfullyear', 'Date.prototype.setUTCFullYear')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.setutcfullyear', 'Date.prototype.setUTCFullYear')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.setUTCFullYear")}}

+
+ +

相关连接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setutchours/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setutchours/index.html new file mode 100644 index 0000000000..16d4989a4b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setutchours/index.html @@ -0,0 +1,94 @@ +--- +title: Date.prototype.setUTCHours() +slug: Web/JavaScript/Reference/Global_Objects/Date/setUTCHours +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setUTCHours +--- +
{{JSRef}}
+ +

The setUTCHours() method sets the hour for a specified date according to universal time, and returns the number of milliseconds since 1 January 1970 00:00:00 UTC until the time represented by the updated {{jsxref("Date")}} instance.

+ +
{{EmbedInteractiveExample("pages/js/date-setutchours.html")}}
+ + + +

Syntax

+ +
dateObj.setUTCHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
+ +

 参数

+ +
+
hoursValue
+
表示小时的整数,取值0到23之间。
+
minutesValue
+
可选参数。表示分钟的整数,取值0到59之间。
+
secondsValue
+
可选参数。表示秒数的整数,取值0到59之间。如果指定了该参数,就要同时指定minutesValue这个参数。
+
msValue
+
可选参数。表示毫秒的整数,取值0到999之间。如果指定了该参数,就要指定minutesValue和secondsValue这两个参数。
+
+ +

返回值

+ +

返回从1970-01-01 00:00:00 UTC 到更新后的日期所表示时间的毫秒数。

+ +

描述

+ +

If you do not specify the minutesValue, secondsValue, and msValue parameters, the values returned from the {{jsxref("Date.prototype.getUTCMinutes()", "getUTCMinutes()")}}, {{jsxref("Date.prototype.getUTCSeconds()", "getUTCSeconds()")}}, and {{jsxref("Date.prototype.getUTCMilliseconds()", "getUTCMilliseconds()")}} methods are used.

+ +

If a parameter you specify is outside of the expected range, setUTCHours() attempts to update the date information in the {{jsxref("Date")}} object accordingly. For example, if you use 100 for secondsValue, the minutes will be incremented by 1 (minutesValue + 1), and 40 will be used for seconds.

+ +

例子

+ +

使用 setUTCHours()

+ +
var theBigDay = new Date();
+theBigDay.setUTCHours(8);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.3.
{{SpecName('ES5.1', '#sec-15.9.5.35', 'Date.prototype.setUTCHours')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setutchours', 'Date.prototype.setUTCHours')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.setutchours', 'Date.prototype.setUTCHours')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.setUTCHours")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setutcmilliseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setutcmilliseconds/index.html new file mode 100644 index 0000000000..b3059be4cb --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setutcmilliseconds/index.html @@ -0,0 +1,84 @@ +--- +title: Date.prototype.setUTCMilliseconds() +slug: Web/JavaScript/Reference/Global_Objects/Date/setUTCMilliseconds +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setUTCMilliseconds +--- +
{{JSRef}}
+ +

setUTCMilliseconds() 方法会根据世界时来设置指定时间的毫秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-setutcmilliseconds.html")}}
+ + + +

语法

+ +
dateObj.setUTCMilliseconds(millisecondsValue)
+ +

参数

+ +
+
millisecondsValue
+
0 - 999 之间的数值,代表毫秒数。
+
+ +

返回值

+ +

返回更新后的时间距 1970 年 1 月 1 日 00:00:00 (UTC) 的毫秒数。

+ +

描述

+ +

如果传递的参数超出了指定的范围,setUTCMilliseconds() 方法会相应地尝试更新储存在 {{jsxref("Date")}}  的时间信息。例如,假设你传递参数的值是 1100,存储在 {{jsxref("Date")}} 的秒数会加 1,然后使用 100 来作为毫秒数。

+ +

示例

+ +

使用 setUTCMilliseconds() 方法

+ +
var theBigDay = new Date();
+theBigDay.setUTCMilliseconds(500);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ES1')}}{{Spec2('ES1')}}初始定义。于 JavaScript 1.3 实现。
{{SpecName('ES5.1', '#sec-15.9.5.29', 'Date.prototype.setUTCMilliseconds')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setutcmilliseconds', 'Date.prototype.setUTCMilliseconds')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.setutcmilliseconds', 'Date.prototype.setUTCMilliseconds')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setUTCMilliseconds")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setutcminutes/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setutcminutes/index.html new file mode 100644 index 0000000000..7ea6d1ed0b --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setutcminutes/index.html @@ -0,0 +1,92 @@ +--- +title: Date.prototype.setUTCMinutes() +slug: Web/JavaScript/Reference/Global_Objects/Date/setUTCMinutes +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setUTCMinutes +--- +
{{JSRef}}
+ +
 
+ +

setUTCMinutes()方法会根据世界协调时(UTC)来设置指定日期的分钟数。

+ +
{{EmbedInteractiveExample("pages/js/date-setutcminutes.html")}}
+ + + +

语法

+ +
dateObj.setUTCMinutes(minutesValue[, secondsValue[, msValue]])
+ +

参数

+ +
+
minutesValue
+
必填,表示要设置的分钟数,是一个介于0和59之间的整数。
+
secondsValue
+
可选参数,表示要设置的秒数,同样也是一个介于0和59之间的整数,如果你传入了这个参数,那么你就必须要传入上一个参数(minutesValue)。
+
msValue
+
可选参数,表示要设置的毫秒数,这是一个介于0和999之间的数字,如果你传入了这个参数,那么你就必须要传入前面两个参数(minutesValuesecondsValue)。
+
+ +

返回值

+ +

返回从UTC时间1970年1月1日0时0分0秒至设置后的时间的毫秒数。

+ +

说明

+ +

如果你没有传入后两个参数(minutesValuemsValue),这两个参数会分别使用{{jsxref("Date.prototype.getUTCSeconds()", "getUTCSeconds()")}}和{{jsxref("Date.prototype.getUTCMilliseconds()", "getUTCMilliseconds()")}}这两个方法返回的值。

+ +

如果你传入的参数值在上文所述范围之外的话,setUTCMinutes()方法会尝试修改日期对象中的其他信息,比如说你为secondsValue这个参数传入了100(译者注:规定范围是[0, 59]),那么第一个参数(minutesValue)就会被加1,而秒数则变成了40。

+ +

例子

+ +

使用 setUTCMinutes()

+ +
var theBigDay = new Date();
+theBigDay.setUTCMinutes(43);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('ES1')}}{{Spec2('ES1')}}首次被定义,在JavaScript 1.3版本被实现。
{{SpecName('ES5.1', '#sec-15.9.5.33', 'Date.prototype.setUTCMinutes')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setutcminutes', 'Date.prototype.setUTCMinutes')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.setutcminutes', 'Date.prototype.setUTCMinutes')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setUTCMinutes")}}

+ +

相关知识

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setutcmonth/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setutcmonth/index.html new file mode 100644 index 0000000000..0e3ffb7782 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setutcmonth/index.html @@ -0,0 +1,90 @@ +--- +title: Date.prototype.setUTCMonth() +slug: Web/JavaScript/Reference/Global_Objects/Date/setUTCMonth +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setUTCMonth +--- +
{{JSRef}}
+ +

setUTCMonth()方法根据通用的时间来设置一个准确的月份

+ +
{{EmbedInteractiveExample("pages/js/date-setutcmonth.html")}}
+ + + +

语法

+ +
dateObj.setUTCMonth(monthValue[, dayValue])
+ +

参数

+ +
+
monthValue
+
一个0-11的整数,代表1月到12月。
+
dayValue
+
可选参数:一个1-31的整数,代表一个月的天数。
+
+ +

返回值

+ +

这个数值是从1970年1月1号 00:00:00到当前时间的毫秒数(国际通用时间)

+ +

描述

+ +

如果你没有明确书写dayValue 这个参数,那么就会从{{jsxref("Date.prototype.getUTCDate()", "getUTCDate()")}} 方法返回对应的数值.

+ +

如果你写了一个超过在规定的范围内的参数. setUTCMonth()就会试图相应的更新时间信息在Data对象中。例如,如果你用15作为monthValue的值,那么年份就会加1,并且月份会变成3.(15=12+3)

+ +

示例

+ +

使用 setUTCMonth()

+ +
var theBigDay = new Date();
+theBigDay.setUTCMonth(11);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范状态注释
{{SpecName('ES1')}}{{Spec2('ES1')}}最初定义并使用在javascript 1.3版本中
{{SpecName('ES5.1', '#sec-15.9.5.39', 'Date.prototype.setUTCMonth')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setutcmonth', 'Date.prototype.setUTCMonth')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.setutcmonth', 'Date.prototype.setUTCMonth')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.setUTCMonth")}}

+
+ +

更多

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setutcseconds/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setutcseconds/index.html new file mode 100644 index 0000000000..fce240e115 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setutcseconds/index.html @@ -0,0 +1,92 @@ +--- +title: Date.prototype.setUTCSeconds() +slug: Web/JavaScript/Reference/Global_Objects/Date/setUTCSeconds +tags: + - 日期 +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setUTCSeconds +--- +
{{JSRef}}
+ +

此 setUTCSeconds() 方法为一个依据国际通用时间的特定日期设置秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-setutcseconds.html")}}
+ + + +

语法

+ +
dateObj.setUTCSeconds(secondsValue[, msValue])
+ +

参数

+ +
+
secondsValue
+
一个在0到59之间的整数,表示秒数。
+
msValue
+
可选参数。一个0到999之间的数字,代表毫秒数。
+
+ +

返回值

+ +

一个毫秒数,表示从国际通用时间1970年00:00:00到设置的时间值之间的时间跨度。

+ +

描述

+ +

如果你没有设置msValue参数的值, 那么返回的值来自{{jsxref("Date.prototype.getUTCMilliseconds()", "getUTCMilliseconds()")}} 方法。

+ +

如果你指定的值超出了范围, setUTCSeconds() 因此会更新{{jsxref("Date")}} 对象中date的相关信息 . 举个例子, 如果你设置secondsValue为100, {{jsxref("Date")}} 对象中的分钟数会增加1, 并且秒数会变成40.

+ +

示例

+ +

使用 setUTCSeconds()

+ +
var theBigDay = new Date();
+theBigDay.setUTCSeconds(20);
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
说明状态备注
{{SpecName('ES1')}}{{Spec2('ES1')}}初始化设定. 从 JavaScript 1.3继承.
{{SpecName('ES5.1', '#sec-15.9.5.31', 'Date.prototype.setUTCSeconds')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.setutcseconds', 'Date.prototype.setUTCSeconds')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.setutcseconds', 'Date.prototype.setUTCSeconds')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.setUTCSeconds")}}

+
+ +

另见

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/setyear/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/setyear/index.html new file mode 100644 index 0000000000..7999ad0022 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/setyear/index.html @@ -0,0 +1,72 @@ +--- +title: Date.prototype.setYear() +slug: Web/JavaScript/Reference/Global_Objects/Date/setYear +tags: + - JavaScript + - 已废弃 + - 日期 +translation_of: Web/JavaScript/Reference/Global_Objects/Date/setYear +--- +
{{JSRef}} {{deprecated_header}}
+ +

setYear() 方法根据一个本地时间为一个确定的日期对象设置年份。由于setYear() 并不设置完整年份("正是千年虫问题"),本方法已经完全被{{jsxref("Date.prototype.setFullYear()","setFullYear()")}} 方法所取代。

+ +

语法

+ +
dateObj.setYear(yearValue)
+ +

参数

+ +
+
yearValue
+
一个整数。
+
+ +

返回值

+ +

介于1970年1月1日 00:00:00 UTC时间与更新后日期的毫秒数。

+ +

描述

+ +

如果yearValue 是介于0到99(包含99)之间的整数,则目标对象 dateObj 的年份被设置为 1900 + yearValue。否则,目标对象 dateObj 的年份被设置为 yearValue.

+ +

例子

+ +

使用 setYear()

+ +

例子前两行(除去声明)将年份设置为1996。 第三行将年份设置为2000.

+ +
var theBigDay = new Date();
+
+theBigDay.setYear(96);
+theBigDay.setYear(1996);
+theBigDay.setYear(2000);
+
+ +

规范

+ + + + + + + + + + +
规范
{{SpecName('ESDraft', '#sec-date.prototype.setyear', 'Date.prototype.setYear')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.setYear")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/todatestring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/todatestring/index.html new file mode 100644 index 0000000000..e2216ea015 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/todatestring/index.html @@ -0,0 +1,72 @@ +--- +title: Date.prototype.toDateString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toDateString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toDateString +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

toDateString() 方法以美式英语和人类易读的形式返回一个日期对象日期部分的字符串。

+ +
{{EmbedInteractiveExample("pages/js/date-todatestring.html")}}
+ + + +

语法

+ +
dateObj.toDateString()
+ +

描述

+ +

{{jsxref("Global_Objects/Date", "Date")}} 对象实例引用一个具体的时间点。调用 {{jsxref("Date.toString", "toString")}} 方法会以美式英语和人类易读的形式返回日期对象的格式化字符串。在 SpiderMonkey 里,该字符串由日期部分(年月日)和其后的时间部分(时分秒及时区)组成。有时需要获取日期部分的字符串,这可以由 toDateString 方法完成。

+ +

The toDateString method is especially useful because compliant engines implementing ECMA-262 may differ in the string obtained from toString for Date objects, as the format is implementation-dependent and simple string slicing approaches may not produce consistent results across multiple engines.

+ +

例子

+ +

例子:toDateString 方法的简单使用

+ +
var d = new Date(1993, 6, 28, 14, 39, 7);
+
+println(d.toString());     // prints Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
+println(d.toDateString()); // prints Wed Jul 28 1993
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 3rd Edition.StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.3', 'Date.prototype.toDateString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.todatestring', 'Date.prototype.toDateString')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.toDateString")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/togmtstring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/togmtstring/index.html new file mode 100644 index 0000000000..4dd44803b4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/togmtstring/index.html @@ -0,0 +1,67 @@ +--- +title: Date.prototype.toGMTString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toGMTString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toGMTString +--- +
{{JSRef("Global_Objects", "Date")}} {{ Deprecated_header() }}
+ +

The toGMTString() method converts a date to a string, using Internet GMT conventions. The exact format of the value returned by toGMTString varies according to the platform and browser, in general it should represent a human readable date string.

+ +

Note: toGMTString is deprecated and should no longer be used, it's only there for backwards compatibility, use {{jsxref("Date.toUTCString", "toUTCString()")}} instead.

+ +

Syntax

+ +
dateObj.toGMTString()
+ +

Examples

+ +

Example: Using toGMTString

+ +

In this example, the toGMTString method converts the date to GMT (UTC) using the operating system's time-zone offset and returns a string value that is similar to the following form. The exact format depends on the platform.

+ +
var today = new Date();
+var str = today.toGMTString();  // deprecated! use toUTCString()
+
+console.log(str);               // Mon, 18 Dec 1995 17:28:35 GMT
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition, but already declared as deprecated.
{{SpecName('ES5.1', '#sec-B.2.6', 'Date.prototype.toGMTString')}}{{Spec2('ES5.1')}}Defined in the (informative) compatibility annex.
{{SpecName('ES6', '#sec-date.prototype.togmtstring', 'Date.prototype.toGMTString')}}{{Spec2('ES6')}}Defined in the (normative) annex for additional features for web browsers.
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Date.toGMTString")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/toisostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/toisostring/index.html new file mode 100644 index 0000000000..b93bd8c745 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/toisostring/index.html @@ -0,0 +1,89 @@ +--- +title: Date.prototype.toISOString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toISOString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toISOString +--- +
{{JSRef}}
+ +

toISOString() 方法返回一个 ISO(ISO 8601 Extended Format)格式的字符串: YYYY-MM-DDTHH:mm:ss.sssZ。时区总是UTC(协调世界时),加一个后缀“Z”标识。

+ +
{{EmbedInteractiveExample("pages/js/date-toisostring.html")}}
+ + + +

语法

+ +
dateObj.toISOString()
+ +

例子

+ +
var today = new Date("05 October 2011 14:48 UTC");
+alert(today.toISOString()); // 返回2011-10-05T14:48:00.000Z
+
+ +

上例使用了非标准字符串的解析,该字符串在某些旧的浏览器(如IE)中可能无法被正确解析。

+ +

Polyfill

+ +

该方法在ECMA-262第5版中被标准化。对于那些不支持此方法的JS引擎可以通过加上下面的代码实现:

+ +
if ( !Date.prototype.toISOString ) {
+  ( function() {
+
+    function pad(number) {
+      if ( number < 10 ) {
+        return '0' + number;
+      }
+      return number;
+    }
+
+    Date.prototype.toISOString = function() {
+      return this.getUTCFullYear() +
+        '-' + pad( this.getUTCMonth() + 1 ) +
+        '-' + pad( this.getUTCDate() ) +
+        'T' + pad( this.getUTCHours() ) +
+        ':' + pad( this.getUTCMinutes() ) +
+        ':' + pad( this.getUTCSeconds() ) +
+        '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
+        'Z';
+    };
+
+  }() );
+}
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ES5.1', '#sec-15.9.5.43', 'Date.prototype.toISOString')}}
+ Implemented in JavaScript 1.8
{{Spec2('ES5.1')}}Initial definition.
{{SpecName('ES6', '#sec-date.prototype.toisostring', 'Date.prototype.toISOString')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.toISOString")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/tojson/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/tojson/index.html new file mode 100644 index 0000000000..689d7b3642 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/tojson/index.html @@ -0,0 +1,73 @@ +--- +title: Date.prototype.toJSON() +slug: Web/JavaScript/Reference/Global_Objects/Date/toJSON +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toJSON +--- +
{{JSRef}}
+ +

toJSON() 方法返回 {{jsxref("Date")}} 对象的字符串形式。

+ +
{{EmbedInteractiveExample("pages/js/date-tojson.html")}}
+ + + +

语法

+ +
dateObj.toJSON()
+
+ +

描述

+ +

{{jsxref("Date")}} 实例引用一个具体的时间点。 调用 toJSON() 返回一个 JSON 格式字符串(使用 {{jsxref("Date.prototype.toISOString()", "toISOString()")}}),表示该日期对象的值。默认情况下,这个方法常用于 {{Glossary("JSON")}}序列化{{jsxref("Date")}}对象。

+ +

样例

+ +

toJSON() 样例

+ +
var date = new Date();
+console.log(date); //Thu Nov 09 2017 18:54:04 GMT+0800 (中国标准时间)
+
+var jsonDate = (date).toJSON();
+console.log(jsonDate); //"2017-11-09T10:51:11.395Z"
+
+var backToDate = new Date(jsonDate);
+console.log(backToDate); //Thu Nov 09 2017 18:54:04 GMT+0800 (中国标准时间)
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范状态说明
{{SpecName('ES5.1', '#sec-15.9.5.44', 'Date.prototype.toJSON')}}{{Spec2('ES5.1')}}首次定义,于 JavaScript 1.8.5 版本实现。
{{SpecName('ES6', '#sec-date.prototype.tojson', 'Date.prototype.toJSON')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.toJSON")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html new file mode 100644 index 0000000000..b5dfe22d12 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/tolocaledatestring/index.html @@ -0,0 +1,155 @@ +--- +title: Date.prototype.toLocaleDateString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

toLocaleDateString() 方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中, localesoptions 参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。

+ +
{{EmbedInteractiveExample("pages/js/date-tolocaledatestring.html")}}
+ + + +

语法

+ +
dateObj.toLocaleDateString([locales [, options]])
+ +

参数

+ +

 查看浏览器兼容性小节,看下哪些浏览器支持 locales 和 options 参数,还可以参看例子: 检测 localesoptions 参数支持情况

+ +

{{page('zh-CN/docs/JavaScript/Reference/Global_Objects/DateTimeFormat','Parameters')}}

+ +

The default value for each date-time component property is undefined, but if the weekday, year, month, day properties are all undefined, then year, month, and day are assumed to be "numeric".

+ +

例子

+ +

例子:使用toLocaleDateString

+ +

没有指定语言环境(locale)时,返回一个使用默认语言环境和格式设置(options)的格式化字符串。

+ +
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
+
+// toLocaleDateString without arguments depends on the implementation,
+// the default locale, and the default time zone
+date.toLocaleDateString();
+// → "12/11/2012" if run in en-US locale with time zone America/Los_Angeles
+ +

例子:检测 localesoptions 参数支持情况

+ +

locales 和 options 参数不是所有的浏览器都支持。为了检测一种实现环境(implementation)是否支持它们,可以使用不合法的语言标签,如果实现环境支持该参数,则会抛出一个 RangeError 异常,反之会忽略参数。

+ +
function toLocaleDateStringSupportsLocales() {
+    try {
+        new Date().toLocaleDateString("i");
+    } catch (e) {
+        return e​.name === "RangeError";
+    }
+    return false;
+}
+
+ +

例子:使用locales

+ +

下例展示了本地化日期格式的一些变化。为了在应用的用户界面得到某种语言的日期格式,必须确保使用 locales 参数指定了该语言(可能还需要设置某些回退语言)。

+ +
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
+
+// formats below assume the local time zone of the locale;
+// America/Los_Angeles for the US
+
+// US English uses month-day-year order
+alert(date.toLocaleDateString("en-US"));
+// → "12/19/2012"
+
+// British English uses day-month-year order
+alert(date.toLocaleDateString("en-GB"));
+// → "20/12/2012"
+
+// Korean uses year-month-day order
+alert(date.toLocaleDateString("ko-KR"));
+// → "2012. 12. 20."
+
+// Arabic in most Arabic speaking countries uses real Arabic digits
+alert(date.toLocaleDateString("ar-EG"));
+// → "٢٠‏/١٢‏/٢٠١٢"
+
+// for Japanese, applications may want to use the Japanese calendar,
+// where 2012 was the year 24 of the Heisei era
+alert(date.toLocaleDateString("ja-JP-u-ca-japanese"));
+// → "24/12/20"
+
+// when requesting a language that may not be supported, such as
+// Balinese, include a fallback language, in this case Indonesian
+alert(date.toLocaleDateString(["ban", "id"]));
+// → "20/12/2012"
+
+ +

例子:使用options

+ +

可以使用 options 参数来自定义 toLocaleDateString 方法返回的字符串。

+ +
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
+
+// request a weekday along with a long date
+var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
+alert(date.toLocaleDateString("de-DE", options));
+// → "Donnerstag, 20. Dezember 2012"
+
+// an application may want to use UTC and make that visible
+options.timeZone = "UTC";
+options.timeZoneName = "short";
+alert(date.toLocaleDateString("en-US", options));
+// → "Thursday, December 20, 2012, GMT"
+
+ +

性能

+ +

当格式化大量日期时,最好创建一个 Intl.DateTimeFormat 对象,然后使用该对象 format 属性提供的方法。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 3rd Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', 'sec-15.9.5.6', 'Date.prototype.toLocaleDateString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.tolocaledatestring', 'Date.prototype.toLocaleDateString')}}{{Spec2('ES6')}} 
ECMAScript Internationalization API Specification, 1st EditionStandardDefines locales and options arguments.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.toLocaleDateString")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/tolocaleformat/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/tolocaleformat/index.html new file mode 100644 index 0000000000..40c72fc476 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/tolocaleformat/index.html @@ -0,0 +1,73 @@ +--- +title: Date.prototype.toLocaleFormat() +slug: Web/JavaScript/Reference/Global_Objects/Date/toLocaleFormat +tags: + - Date + - 非标准 +translation_of: Archive/Web/JavaScript/Date.toLocaleFormat +--- +
{{JSRef}} {{non-standard_header}}
+ +

非标准方法 toLocaleFormat() 按特定的格式将一个日期转换成一个字符串。 {{jsxref("Global_Objects/DateTimeFormat", "Intl.DateTimeFormat")}} 是符合标准的格式化日期的替代方法。另见更新的(newer)版本的 {{jsxref("Date.prototype.toLocaleDateString()")}}方法.

+ +

语法

+ +
dateObj.toLocaleFormat(formatString)
+ +

参数

+ +
+
formatString
+
与C语言中的 strftime() 方法的参数形式要求相同的格式字符串。
+
+ +

描述

+ +

toLocaleFormat() 方法通过格式化生成的日期或时间提供了更好的软件层面的控制(provides greater software control over the formatting of the generated date and/or time)。用操作系统的地点来月份和星期几的名称本地化。然而,However, ordering of the day and month and other localization tasks are not handled automatically since you have control over the order in which they occur. You should take care that the format string is localized properly according to the user's system settings. Be aware that the locale used is not necessarily the same as the locale of the browser.

+ +

Extension and XULRunner developers should know that just loading the format string from a .dtd or .properties file using a chrome://somedomain/locale/somefile.ext URI should be avoided, as the .dtd/.properties file and the toLocaleFormat() method does not not necessarily use the same locale, which could result in odd looking or even ambiguous or unreadable dates.

+ +

Also note that the behavior of the used locale depends on the platform, and the user might customize the locale used, so using the system locale the choose the format string might in some cases not even be adequate. You might consider using some of the more general toLocale* methods of the {{jsxref("Global_Objects/Date", "Date")}} object or doing your own custom localization of the date to be displayed using some of the get* methods of the {{jsxref("Global_Objects/Date", "Date")}} object instead of using this method.

+ +

示例

+ +

Using toLocaleFormat()

+ +
var today = new Date();
+var date = today.toLocaleFormat('%A, %B %e, %Y'); // Bad example
+
+ +

In this example, toLocaleFormat() returns a string such as "Wednesday, October 3, 2007". Note that the format string in this example is not properly localized, which will result in the problems described above.

+ +

腻子(Polyfill)

+ +

When using the DateJS library you can polyfill {{jsxref("Date.prototype.toLocaleDateString()")}} like this:

+ +
if (!Date.prototype.toLocaleFormat) {
+    (function() {
+        Date.prototype.toLocaleFormat = function(formatString) {
+            return this.format(formatString);
+        };
+    }());
+}
+ +

标准

+ +

不属于任何标准。在JavaScript 1.6中被实现。

+ +

兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.toLocaleFormat")}}

+
+ +

另见

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/tolocalestring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/tolocalestring/index.html new file mode 100644 index 0000000000..75e2834be5 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/tolocalestring/index.html @@ -0,0 +1,161 @@ +--- +title: Date.prototype.toLocaleString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toLocaleString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toLocaleString +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

toLocaleString() 方法返回该日期对象的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中, locales 和 options 参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。

+ +
{{EmbedInteractiveExample("pages/js/date-tolocalestring.html")}}
+ + + +

语法

+ +
dateObj.toLocaleString([locales [, options]])
+ +

参数

+ +

查看浏览器兼容性小节,看下哪些浏览器支持 locales 和 options 参数,还可以参看例子:检测 localesoptions 参数支持情况

+ +

{{page('zh-CN/docs/JavaScript/Reference/Global_Objects/DateTimeFormat','Parameters')}}

+ +

每个日期时间组件的默认值都是undefined, 但是如果 weekday, year, month, day, hour, minute, second 属性都是 undefined, 那么 year, month, day, hour, minute 和 second 的值都被认为是 "numeric".

+ +

返回值

+ +

根据当地语言规定返回代表着时间的字符串。

+ +

例子

+ +

例子:使用 toLocaleString

+ +

没有指定语言环境(locale)时,返回一个使用默认语言环境和格式设置(options)的格式化字符串。

+ +
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
+
+// toLocaleString 不包含参数的返回值取决于实现,
+// 默认的区域(locale),和默认的时区(time zone)
+date.toLocaleString();
+// → 如果是在en-US区域和America/Los_Angeles时区运行返回值为"12/11/2012, 7:00:00 PM"
+ +

例子:检测 locales 和 options 参数支持情况

+ +

locales 和 options 参数不是所有的浏览器都支持。为了检测一种实现环境(implementation)是否支持它们,可以使用不合法的语言标签,如果实现环境支持该参数,则会抛出一个 RangeError 异常,反之会忽略参数。

+ +
function toLocaleStringSupportsLocales() {
+    try {
+        new Date().toLocaleString("i");
+    } catch (e) {
+        return e​.name === "RangeError";
+    }
+    return false;
+}
+
+ +

例子:使用 locales 参数

+ +

下例展示了本地化日期格式的一些变化。为了在应用的用户界面得到某种语言的日期和时间格式,必须确保使用 locales 参数指定了该语言(可能还需要设置某些回退语言)。

+ +
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
+
+//假定本地时区是 America/Los_Angeles(美国时区)
+//en-US(美利坚英语)使用 month-day-year 的顺序展示年月日
+alert(date.toLocaleString("en-US"));
+// → "12/19/2012, 7:00:00 PM"
+
+// en-GB(不列颠英语)使用 day-month-year 顺序展示年月日
+alert(date.toLocaleString("en-GB"));
+// → "20/12/2012 03:00:00"
+
+// 韩语使用 year-month-day 顺序展示年月日
+alert(date.toLocaleString("ko-KR"));
+// → "2012. 12. 20. 오후 12:00:00"
+
+// 大多数阿拉伯语国家的阿拉伯语使用阿拉伯数字
+alert(date.toLocaleString("ar-EG"));
+// → "٢٠‏/١٢‏/٢٠١٢ ٥:٠٠:٠٠ ص"
+
+//在日本,应用可能想要使用日本日历,
+//2012 是平成24年(平成是是日本天皇明仁的年号,由1989年1月8日起开始计算直至现在)
+alert(date.toLocaleString("ja-JP-u-ca-japanese"));
+// → "24/12/20 12:00:00"
+
+//当请求一个语言可能不支持,如巴厘(ban),若有备用的语言印尼语(id),
+//那么将使用印尼语(id)
+alert(date.toLocaleString(["ban", "id"]));
+// → "20/12/2012 11.00.00"
+
+ +

例子:使用 options 参数

+ +

可以使用 options 参数来自定义 toLocaleString 方法返回的字符串。

+ +
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
+
+//请求参数(options)中包含参数星期(weekday),并且该参数的值为长类型(long)
+var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
+alert(date.toLocaleString("de-DE", options));
+// → "Donnerstag, 20. Dezember 2012"
+
+//一个应用使用 世界标准时间(UTC),并且UTC使用短名字(short)展示
+options.timeZone = "UTC";
+options.timeZoneName = "short";//若不写这一行那么仍然显示的是世界标准时间;但是GMT三个字母不会显示
+alert(date.toLocaleString("en-US", options));
+// → "Thursday, December 20, 2012, GMT"
+
+// 使用24小时制
+alert(date.toLocaleString("en-US", {hour12: false}));
+// → "12/19/2012, 19:00:00"
+
+ +

性能

+ +

当格式化大量日期时,最好创建一个 Intl.DateTimeFormat 对象,然后使用该对象 format 属性提供的方法。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', 'sec-15.9.5.5', 'Date.prototype.toLocaleString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.tolocalestring', 'Date.prototype.toLocaleString')}}{{Spec2('ES6')}} 
ECMAScript Internationalization API Specification, 1st EditionStandardDefines locales and options arguments.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.toLocaleString")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/tolocaletimestring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/tolocaletimestring/index.html new file mode 100644 index 0000000000..2a52a021e7 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/tolocaletimestring/index.html @@ -0,0 +1,151 @@ +--- +title: Date.prototype.toLocaleTimeString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

The toLocaleTimeString() 方法返回该日期对象时间部分的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。在旧版本浏览器中, locales 和 options 参数被忽略,使用的语言环境和返回的字符串格式是各自独立实现的。

+ +
{{EmbedInteractiveExample("pages/js/date-tolocaletimestring.html")}}
+ + + +

语法

+ +
dateObj.toLocaleTimeString([locales [, options]])
+ +

参数

+ +

查看浏览器兼容性小节,看下哪些浏览器支持 locales 和 options 参数,还可以参看例子:检测 localesoptions 参数支持情况

+ +

{{page('zh-US/docs/JavaScript/Reference/Global_Objects/DateTimeFormat','Parameters')}}

+ +

The default value for each date-time component property is undefined, but if the hour, minute, second properties are all undefined, then hour, minute, and second are assumed to be "numeric".

+ +

例子

+ +

例子:使用 toLocaleTimeString

+ +

没有指定语言环境(locale)时,返回一个使用默认语言环境和格式设置(options)的格式化字符串。

+ +
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
+
+// toLocaleTimeString without arguments depends on the implementation,
+// the default locale, and the default time zone
+alert(date.toLocaleTimeString());
+// → "7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
+ +

例子:检测 localesoptions 支持情况

+ +

locales 和 options 参数不是所有的浏览器都支持。为了检测一种实现环境(implementation)是否支持它们,可以使用不合法的语言标签,如果实现环境支持该参数,则会抛出一个 RangeError 异常,反之会忽略参数。

+ +
function toLocaleTimeStringSupportsLocales() {
+    try {
+        new Date().toLocaleTimeString("i");
+    } catch (e) {
+        return e​.name === "RangeError";
+    }
+    return false;
+}
+
+ +

例子:使用 locales 参数

+ +

下例展示了本地化时间格式的一些变化。为了在应用的用户界面得到某种语言的时间格式,必须确保使用 locales 参数指定了该语言(可能还需要设置某些回退语言)。

+ +
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
+
+// formats below assume the local time zone of the locale;
+// America/Los_Angeles for the US
+
+// US English uses 12-hour time with AM/PM
+alert(date.toLocaleTimeString("en-US"));
+// → "7:00:00 PM"
+
+// British English uses 24-hour time without AM/PM
+alert(date.toLocaleTimeString("en-GB"));
+// → "03:00:00"
+
+// Korean uses 12-hour time with AM/PM
+alert(date.toLocaleTimeString("ko-KR"));
+// → "오후 12:00:00"
+
+// Arabic in most Arabic speaking countries uses real Arabic digits
+alert(date.toLocaleTimeString("ar-EG"));
+// → "٧:٠٠:٠٠ م"
+
+// when requesting a language that may not be supported, such as
+// Balinese, include a fallback language, in this case Indonesian
+alert(date.toLocaleTimeString(["ban", "id"]));
+// → "11.00.00"
+
+ +

例子:使用 options 参数

+ +

可以使用 options 参数来自定义 toLocaleTimeString 方法返回的字符串。

+ +
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
+
+// an application may want to use UTC and make that visible
+var options = {timeZone: "UTC", timeZoneName: "short"};
+alert(date.toLocaleTimeString("en-US", options));
+// → "3:00:00 AM GMT"
+
+// sometimes even the US needs 24-hour time
+alert(date.toLocaleTimeString("en-US", {hour12: false}));
+// → "19:00:00"
+
+ +

性能

+ +

当格式化大量日期时,最好创建一个 Intl.DateTimeFormat 对象,然后使用该对象 format 属性提供的方法。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 3rd Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.7', 'Date.prototype.toLocaleTimeString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.tolocalestring', 'Date.prototype.toLocaleTimeString')}}{{Spec2('ES6')}} 
ECMAScript Internationalization API Specification, 1st EditionStandardDefines locales and options arguments.
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.toLocaleTimeString")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/tosource/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/tosource/index.html new file mode 100644 index 0000000000..dbcae1c739 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/tosource/index.html @@ -0,0 +1,50 @@ +--- +title: Date.prototype.toSource() +slug: Web/JavaScript/Reference/Global_Objects/Date/toSource +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toSource +--- +
{{JSRef}} {{non-standard_header}}
+ +

toSource() 返回表示源代码的字符串。

+ +

语法

+ +
dateObj.toSource()
+Date.toSource()
+ +

返回值

+ +

 表示{{jsxref("Global_Objects/Date", "Date")}} 源代码的字符串

+ +

描述

+ +

toSource() 返回以下参数:

+ + + +

此方法通常由JavaScript内部调用,而不是在代码中显式调用。

+ +

规范

+ +

暂无标准, 在 JavaScript 1.3中实现.

+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.toSource")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/tostring/index.html new file mode 100644 index 0000000000..ce97ceafc5 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/tostring/index.html @@ -0,0 +1,90 @@ +--- +title: Date.prototype.toString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toString +--- +
{{JSRef}}
+ +

toString() 方法返回一个字符串,表示该{{jsxref("Date")}}对象。

+ +
{{EmbedInteractiveExample("pages/js/date-tostring.html")}}
+ + + +

语法

+ +
dateObj.toString()
+ +

参数

+ +

+ +

描述

+ +

{{jsxref("Date")}}对象覆盖了 {{jsxref("Object")}} 对象的 toString() 方法;它不是继承自 {{jsxref("Object.prototype.toString()")}}。对于  {{jsxref("Date")}} 对象,toString() 方法返回一个表示该对象的字符串。

+ +

toString 方法总是返回一个美式英语日期格式的字符串。

+ +

当一个日期对象被用来作为文本值或用来进行字符串连接时,toString 方法会被自动调用。

+ +

toString() 是通用函数。如果不是{{jsxref("Date")}}实例,则 返回"Invalid Date"。

+ +

例子

+ +

例子: 使用 toString 方法

+ +

下例把一个{{jsxref("Date")}}对象的 toString 返回值赋给 myVar:

+ +
var x = new Date();
+myVar = x.toString(); // 把类似于下面格式的值赋给 myVar,
+// Fri Apr 26 2019 11:46:17 GMT+0800 (中国标准时间)
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.0.
{{SpecName('ES5.1', '#sec-15.9.5.2', 'Date.prototype.toLocaleTimeString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.tostring', 'Date.prototype.toString')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.tostring', 'Date.prototype.toString')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ +
+ + +

{{Compat("javascript.builtins.Date.toString")}}

+
+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/totimestring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/totimestring/index.html new file mode 100644 index 0000000000..0a05806085 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/totimestring/index.html @@ -0,0 +1,73 @@ +--- +title: Date.prototype.toTimeString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toTimeString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toTimeString +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

toTimeString() 方法以人类易读形式返回一个日期对象时间部分的字符串,该字符串以美式英语格式化。

+ +
{{EmbedInteractiveExample("pages/js/date-totimestring.html")}}
+ + + +

语法

+ +
dateObj.toTimeString()
+ +

描述

+ +

{{jsxref("Global_Objects/Date", "Date")}} 对象的实例引用一个具体的时间点。 调用 {{jsxref("Date.toString", "toString")}} 方法以美式英语和人类易读的形式,返回日期对象的格式化字符串。在 SpiderMonkey 里,该字符串由日期部分(年月日)和其后的时间部分(时分秒和时区)组成。有时会需要获取时间部分的字符串,这可以由 toTimeString 方法完成。

+ +

The toTimeString method is especially useful because compliant engines implementing ECMA-262 may differ in the string obtained from toString for Date objects, as the format is implementation-dependent; simple string slicing approaches may not produce consistent results across multiple engines.

+ +

例子

+ +

例子:toTimeString 方法的简单使用

+ +
var d = new Date(1993, 6, 28, 14, 39, 7);
+
+println(d.toString());     // prints Wed Jul 28 1993 14:39:07 GMT-0600 (PDT)
+println(d.toTimeString()); // prints 14:39:07 GMT-0600 (PDT)
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 3rd EditionStandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.4', 'Date.prototype.toTimeString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.totimestring', 'Date.prototype.toTimeString')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.toTimeString")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/toutcstring/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/toutcstring/index.html new file mode 100644 index 0000000000..4889075b1c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/toutcstring/index.html @@ -0,0 +1,76 @@ +--- +title: Date.prototype.toUTCString() +slug: Web/JavaScript/Reference/Global_Objects/Date/toUTCString +translation_of: Web/JavaScript/Reference/Global_Objects/Date/toUTCString +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

toUTCString() 方法把一个日期转换为一个字符串,使用UTC时区。

+ +
{{EmbedInteractiveExample("pages/js/date-toutcstring.html")}}
+ + + +

语法

+ +
dateObj.toUTCString()
+ +

返回值

+ +

返回使用UTC时区表示给定日期的字符串

+ +

 

+ +

描述

+ +

toUTCString 的返回值是一个使用UTC时区的易读格式字符串。返回值的格式可能随平台而变化。通常返回值是一个 RFC-1123 格式的时间戳,这是一个 RFC-822 时间戳的小幅更新版。

+ +

例子

+ +

例子:使用toUTCString

+ +
var today = new Date();
+var UTCstring = today.toUTCString();
+// Mon, 03 Jul 2006 21:44:38 GMT
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.3StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.5.42', 'Date.prototype.toUTCString')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.toutcstring', 'Date.prototype.toUTCString')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.toUTCString")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/utc/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/utc/index.html new file mode 100644 index 0000000000..0be526200f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/utc/index.html @@ -0,0 +1,104 @@ +--- +title: Date.UTC() +slug: Web/JavaScript/Reference/Global_Objects/Date/UTC +tags: + - Date + - JavaScript + - Method +translation_of: Web/JavaScript/Reference/Global_Objects/Date/UTC +--- +
{{JSRef("Global_Objects", "Date")}}
+ +

Date.UTC() 方法接受的参数同日期构造函数接受最多参数时一样,返回从1970-1-1 00:00:00 UTC到指定日期的的毫秒数。

+ +
{{EmbedInteractiveExample("pages/js/date-utc.html")}}
+ + + +

语法

+ +
Date.UTC(year,month[,date[,hrs[,min[,sec{{mediawiki.external(',ms')}}]]]]) 
+ +

参数

+ +
+
year
+
1900 年后的某一年份。
+
month
+
0 到 11 之间的一个整数,表示月份。
+
date
+
1 到 31 之间的一个整数,表示某月当中的第几天。
+
hrs
+
0 到 23 之间的一个整数,表示小时。
+
min
+
0 到 59 之间的一个整数,表示分钟。
+
sec
+
0 到 59 之间的一个整数,表示秒。
+
ms
+
0 到 999 之间的一个整数,表示毫秒。
+
+ +

描述

+ +

UTC 方法接受以逗号隔开的日期参数,返回1970-1-1 00:00:00 UTC到指定的日期之间的毫秒数。

+ +

你应该指定一个完整格式的年份,如 1998。如果年份被指定为 0 到 99 之间,则该方法会将年份转换为 20 世纪的一个年份(即 1900 + year),例如,指定为 95, 则年份为 1995。

+ +

UTC 方法与 Date 有两点不同:

+ + + +

如果有一个指定的参数超出其合理范围,则 UTC 方法会通过更新其他参数直到该参数在合理范围内。例如,为月份指定 15,则年份将会加 1,然后月份将会使用 3。

+ +

由于 UTCDate(日期对象)的一个静态方法,所以应该在 Date 上直接调用,就像 Date.UTC(),而不要把它作为创建的日期对象的方法。

+ +

例子

+ +

例子:使用 Date.UTC

+ +

下面的语句使用 UTC 时间代替本地时间创建了一个日期对象。

+ +
var utcDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0));
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES5.1', '#sec-15.9.4.3', 'Date.UTC')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.utc', 'Date.UTC')}}{{Spec2('ES6')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.UTC")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/valueof/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/valueof/index.html new file mode 100644 index 0000000000..9871342a43 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/valueof/index.html @@ -0,0 +1,86 @@ +--- +title: Date.prototype.valueOf() +slug: Web/JavaScript/Reference/Global_Objects/Date/valueOf +tags: + - Date + - JavaScript + - 原型 + - 参考 + - 方法 + - 日期 +translation_of: Web/JavaScript/Reference/Global_Objects/Date/valueOf +--- +
{{JSRef}}
+ +

valueOf() 方法返回一个 {{jsxref("Date")}} 对象的原始值。

+ +
{{EmbedInteractiveExample("pages/js/date-valueof.html")}}
+ +

语法

+ +
dateObj.valueOf()
+ +

返回值

+ +

从1970年1月1日0时0分0秒(UTC,即协调世界时)到该日期的毫秒数。

+ +

描述

+ +

valueOf 方法返回以数值格式表示的一个 Date 对象的原始值,从1970年1月1日0时0分0秒(UTC,即协调世界时)到该日期对象所代表时间的毫秒数。

+ +

该方法的功能和 {{jsxref("Date.prototype.getTime()")}} 方法一样。

+ +

该方法通常在 JavaScript 内部被调用,而不是在代码中显式调用。

+ +

例子

+ +

使用 valueOf()

+ +
var x = new Date(56, 6, 17);
+var myVar = x.valueOf();      // assigns -424713600000 to myVar
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition.
{{SpecName('ES5.1', '#sec-15.9.5.8', 'Date.prototype.valueOf')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-date.prototype.valueof', 'Date.prototype.valueOf')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-date.prototype.valueof', 'Date.prototype.valueOf')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.valueOf")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf