Date.UTC()
与 Date 构造函数类似,Date.UTC() 静态方法接受表示日期和时间部分的参数,但将它们视为 UTC。它返回自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数。
¥The Date.UTC() static method accepts parameters representing the date and time components similar to the Date constructor, but treats them as UTC. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
Try it
语法
参数
¥Parameters
year-
代表年份的整数值。
0到99的值映射到1900到1999年。所有其他值均为实际年份。参见 example。 monthIndexOptional-
代表月份的整数值,从
0开始(一月)到11(十二月)。默认为0。 dayOptional-
代表月份中的某天的整数值。默认为
1。 hoursOptional-
:
0和23之间的整数值,代表一天中的小时。默认为0。 minutesOptional-
表示时间的分钟段的整数值。默认为
0。 secondsOptional-
代表时间第二段的整数值。默认为
0。 millisecondsOptional-
表示时间的毫秒段的整数值。默认为
0。
返回值
描述
¥Description
0 和 99 之间的年份转换为 20 世纪 (1900 + year) 中的年份。例如,95 转换为年份 1995。
¥Years between 0 and 99 are converted to a year in the 20th century (1900 + year). For example, 95 is converted to the year 1995.
UTC() 方法与 Date() 构造函数在三个方面有所不同:
¥The UTC() method differs from the Date() constructor in three ways:
Date.UTC()使用世界时间而不是当地时间。Date.UTC()以数字形式返回时间值,而不是创建Date对象。- 当传递单个数字时,
Date.UTC()将其解释为年份而不是时间戳。
如果参数超出预期范围,UTC() 方法将更新其他参数以适应该值。例如,如果 15 用于 monthIndex,年份将增加 1 (year + 1),月份将使用 3。
¥If a parameter is outside of the expected range, the UTC() method updates the other parameters to accommodate the value. For example, if 15 is used for monthIndex, the year will be incremented by 1 (year + 1) and 3 will be used for the month.
因为 UTC() 是 Date 的静态方法,所以你始终将其用作 Date.UTC(),而不是用作你创建的 Date 对象的方法。
¥Because UTC() is a static method of Date, you always use it as Date.UTC(), rather than as a method of a Date object you created.
示例
使用 Date.UTC()
带一个参数的 Date.UTC() 的行为
¥Behavior of Date.UTC() with one argument
Date.UTC() 在传递一个参数时过去常常具有不一致的行为,因为实现仅使行为与 Date() 构造函数保持一致,而 Date() 构造函数不会将单个参数解释为年份数字。现在需要实现将省略的 monthIndex 视为 0,而不是将其强制为 NaN。
¥Date.UTC() when passed one argument used to have inconsistent behavior, because implementations only kept the behavior consistent with the Date() constructor, which does not interpret a single argument as the year number. Implementations are now required to treat omitted monthIndex as 0, instead of coercing it to NaN.
Date.UTC(2017); // 1483228800000
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-date.utc |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also