javascript setUTCMilliseconds错误吗?否则,我';我错了
我是否错误地实现了setutcmillesons?我输入的任何值的日期都不正确。这里只是一个错误值的示例。我所有的测试数据在JS中解析为5月24日(未来),但在C#或使用快速在线转换工具时,我的UTS MS是正确的 有什么想法吗javascript setUTCMilliseconds错误吗?否则,我';我错了,javascript,date,utc,milliseconds,Javascript,Date,Utc,Milliseconds,我是否错误地实现了setutcmillesons?我输入的任何值的日期都不正确。这里只是一个错误值的示例。我所有的测试数据在JS中解析为5月24日(未来),但在C#或使用快速在线转换工具时,我的UTS MS是正确的 有什么想法吗 function parseDate(epoch) { var d = new Date(); //tried this too, but it shows me the actual epoch 1970 date instead //
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)
从一个类似的问题:
var utcMilliseconds = 1234567890000;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCMilliseconds(utcMilliseconds);
参见类似问题中的。:
var utcMilliseconds = 1234567890000;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCMilliseconds(utcMilliseconds);
请参见。以秒为单位将UTC时间转换为本地日期对象:
function makeUTC(secs) {
return new Date(Date.UTC(1970,0,1,0,0, secs, 0));
}
请注意,历元为1970-01-01T00:00:00.0Z,以秒为单位将UTC时间转换为本地日期对象:
function makeUTC(secs) {
return new Date(Date.UTC(1970,0,1,0,0, secs, 0));
}
请注意,历元是1970-01-01T00:00:00.0Z只需使用以毫秒为数字的构造函数即可:
> new Date(1336423503 * 1000)
2012-05-07T20:45:03.000
无需创建日期对象并在其后设置utcmillesons。只需使用以毫秒为数字的构造函数即可:
> new Date(1336423503 * 1000)
2012-05-07T20:45:03.000
无需创建日期对象并在其后设置单位毫秒。您传递的值不是历元,而是从历元开始的时间。是的,我理解数字的含义。我在这里使用它就像是epochtime你传递的值不是一个纪元,而是一个从纪元开始的时间。是的,我理解这个数字的意思。我在这里使用它就像epochtimeyeah我试过了,它只是给我显示了1970年的纪元日期。啊,好的,确实需要0,但没有注意到它的秒数,不是毫秒。因为时间是以秒为单位传递的,你可以这样做:
d.setUTCSeconds(seconds,0)
是的,我试过了,它只是给我显示了1970年的纪元日期。啊,好的,确实需要0,但是没有注意到它的秒数,而不是毫秒。因为时间是以秒为单位传递的,所以您可以执行:d.setUTCSeconds(seconds,0)