Date.UTC 函数

返回协调通用时间(UTC)1970年1月1日午夜与所指定的日期之间的毫秒数。

语法

Date.UTC(year, month, day[, hours[, minutes[, seconds[,ms]]]])

参数

year

必要参数。为了确保跨世纪日期的精确性,需要指定完整的年份。如果year处于0到99之间,则year就被假定为1999+year

month

必要参数。月份,用从0到11的整数表示(1月至12月)。

day

必要参数。日期,用从1到31的整数表示。

hours

可选参数。如果提供了minutes,则必须提供此参数。一个指定小时的、从0到23的整数(午夜到11pm)。

minutes

可选参数。如果提供了seconds,则必须提供此参数。一个指定分钟的、从0到59的整数。

seconds

可选参数。如果提供了milliseconds,则必须提供此参数。一个指定秒的,从0到59的整数。

ms

可选参数。一个指定毫秒的,从0到999的整数。

备注

Date.UTC函数返回从UTC 1970年1月1日午夜到所提供日期之间的毫秒数。此返回值可用在setTime方法和Date对象构造函数中。如果参数值大于其范围世为负数,则其他存储的值都将得到相应的修改。例如,如果指定150秒,则JavaScript将该数字重新定义为2分30秒。

Date.UTC函数和接受日期的Date对象构造函数之间的差别在于:Date.UTC采用UTC,而Date对象构造函数采用当地时间。

示例

下面的示例演示了Date.UTC函数的用法。

// Determine the milliseconds per day.
 var MinMilli = 1000 * 60;
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
var date = new Date("June 1, 1990");
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDay();
var newDay = new Date("January 16, 2020");
var yeartoday = newDay.getUTCFullYear();
var monthtoday = newDay.getUTCMonth();
var dayofmonthtoday = newDay.getUTCDate();
// Get the milliseconds since 1/1/1970 UTC.
var t1 = Date.UTC(year, month - 1, day)
var t2 = Date.UTC(yeartoday, monthtoday, dayofmonthtoday);
// Determine the difference in days.
var days = (t2 - t1) / DyMilli;
document.write(days);
// Output: 10848

必备条件

在以下文档模式中受支持:怪异模式、Internet Explorer 6标准模式、Internet Explorer 7标准模式、Internet Explorer 8标准模式、Internet Explorer 9标准模式、Internet Explorer 10标准模式、Internet Explorer 11标准模式。应用商店应用(Windows 8和Windows Phone 8.1)中也受支持。请参阅版本信息

如果你喜欢这篇文章,敬请给站长打赏↑

除特别注明外,本站所有文章均为本站站长原译,转载请注明出处。