2

我正在使用从此处下载的最新引导程序 datetimepicker,但是在使用 API 时在获取正确的 UTC 日期时间时遇到问题。

var dateTimePicker = jQuery('.datetimepicker').datetimepicker();
dateTimePicker.on('changeDate', function(e) {

var eventTarget = jQuery(e.target);

var picker = eventTarget.data('datetimepicker');

console.log( "getDate: " + picker.getDate());
console.log( "getLocalDate: " +  picker.getLocalDate());
console.log( "getLocalDate to UTC: " +  picker.getLocalDate().toUTCString());

输出

 getDate: Wed Apr 24 2013 05:53:40 GMT+0530 (IST)

 getLocalDate: Wed Apr 24 2013 00:23:40 GMT+0530 (IST)

 getLocalDate to UTC: Tue, 23 Apr 2013 18:53:40 GMT

这表明 getLocalDate 在我的时区 GMT +0530 中返回正确的日期时间,并且 getLocalDate.toUTCString 为我的本地日期时间返回相应的 UTC 日期时间。但是 getDate 显然没有按照文档返回正确的 UTC 日期时间

   // Considering you are on a GMT-3 timezone and the input contains '2000-01-17 10:p'
   var localDate = picker.getLocalDate(); // localDate === 2000-01-17 07:00
   var utcDate = picker.getDate(); // utcDate === 2000-01-17 10:00

在源代码中,我发现以下我无法理解的代码。

 function UTCDate() {
   return new Date(Date.UTC.apply(Date, arguments));
 }

谁能让我理解代码片段new Date(Date.UTC.apply(Date, arguments))的工作原理以及它应该返回什么?

在撰写本文时,在我的时区 GMT +0530 中是Wed Apr 24 00:19,上面提到的 UTCDate 函数为此返回Wed Apr 24 2013 05:48:49 GMT+0530 (IST),这显然不是对应的 UTC DateTime 在我的时区中的日期时间。

4

1 回答 1

1

Date.UTC 返回指定日期与 1970 年 1 月 1 日午夜之间的毫秒数。

apply 方法是一种 javascript 方法,可让您调用另一个方法并将父方法的参数传递给它。

我在想这是试图通过将 Date.UTC() 作为初始化程序传递给它来创建一个新日期,使用传递给 UTCDate() 方法的参数。

于 2013-04-30T20:06:44.160 回答