目录

JavaScript Date.UTC()

示例

获取某个日期与 1970 年 1 月 1 日之间的毫秒数:

let ms = Date.UTC(2020, 02, 30);
亲自试一试 »

下面有更多 "亲自试一试" 示例。


描述

Date.UTC() 方法根据 UTC 返回指定日期与 1970 年 1 月 1 日午夜之间的毫秒数。

笔记

UTC(通用协调时间)是世界时间标准设定的时间。

UTC 时间与 GMT 时间(格林威治标准时间)相同。


浏览器支持

Date.UTC()是 ECMAScript1 (ES1) 功能。

所有浏览器均完全支持 ES1 (JavaScript 1997):

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

语法

Date.UTC( year, month, day, hours, minutes, seconds, millisec)

Date.UTC() 是 Date 对象的静态方法。

您不能在 d.UTC() 这样的日期上使用它

语法始终为 Date.UTC()。

参数值

Parameter Description
year Required. A four-digit value representing the year, negative values are allowed
month Required. An integer representing the month

Expected values are 0-11, but other values are allowed:

  • -1 will result in the last month of the previous year
  • 12 will result in the first month of the next year
  • 13 will result in the second month of the next year
day Optional. An integer representing the day of month

Expected values are 1-31, but other values are allowed:

  • 0 will result in the last hour of the previous month
  • -1 will result in the hour before the last hour of the previous month

If the month has 31 days:

  • 32 will result in the first day of the next month

If the month has 30 days:

  • 32 will result in the second day of the next month
hour Optional. Default 0. An integer representing the hour.

Expected values are 0-23, but other values are allowed:

  • -1 will result in the last hour of the previous day
  • 24 will result in the first hour of the next day
min Optional. Default 0. An integer representing the minutes.

Expected values are 0-59, but other values are allowed:

  • -1 will result in the last minute of the previous hour
  • 60 will result in the first minute of the next hour
sec Optional. Default 0. An integer representing the seconds

Expected values are 0-59, but other values are allowed:

  • -1 will result in the last second of the previous minute
  • 60 will result in the first second of the next minute
millisec Optional. Default 0. An integer representing the milliseconds

Expected values are 0-999, but other values are allowed:

  • -1 will result in the last millisecond of the previous second
  • 1000 will result in the first millisecond of the next second


技术细节

返回值: 一个数字,表示指定日期时间与 1970 年 1 月 1 日午夜之间的毫秒数
JavaScript 版本: ECMA脚本1

更多示例

示例

使用 UTC 时间而不是本地时间创建日期对象:

let d = new Date(Date.UTC(2020, 02, 30));
亲自试一试 »