2012-05-09 23 views
1

我是否错误地实现了setUTCMilliseconds?对于我输入的任何值,我都会得到错误的日期。下面只是一个错误值的例子。我所有的测试数据都是在JS中的5月24日(未来的未来)解决,但在C#或使用快速在线转换工具中,我的UTS MS是正确的。javascript setUTCMilliseconds是错误的?否则,我错了

有什么想法?

function parseDate(epoch) { 
    var d = new Date(); 

    //tried this too, but it shows me the actual epoch 1970 date instead 
    //var d = new Date(0); 

    //EDIT: this should be seconds in combination with Date(0) 
    d.setUTCMilliseconds(parseInt(epoch)); 

    return d.toString(); 
} 

// 1336423503 -> Should be Mon May 07 2012 13:45:03 GMT-7 

// javascript says 
Thu May 24 2012 05:03:21 GMT-0700 (Pacific Daylight Time) 
+1

你传递的价值不是一个时代,它是自**时代以来的时间**。 – RobG

+0

是的,我明白数字的含义。我在这里使用它像epochtime – FlavorScape

回答

2

从一个类似的问题:

var utcMilliseconds = 1234567890000; 
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch 
d.setUTCMilliseconds(utcMilliseconds); 

Convert UTC Epoch to local date with javascript

+0

是的,我试过了,它只是显示了我在1970年的时代日期。 – FlavorScape

+0

啊,好吧,确实需要0,但没有注意到它的秒,而不是ms。 – FlavorScape

+0

由于时间以秒为单位传递,您可以这样做:'d.setUTCSeconds(seconds,0)' – RobG

1

要转换UTC时间,以秒为本地日期的对象:

function makeUTC(secs) { 
    return new Date(Date.UTC(1970,0,1,0,0, secs, 0)); 
} 

注意,时代为1970-01-01T00:00:00.0Z

1

只需使用Date()构造与毫秒作为一个数字:

> new Date(1336423503 * 1000) 
2012-05-07T20:45:03.000 

有没有必要以后创建一个Date对象和setUTCMilliseconds。

相关问题