我已经检查过了,使用momentjs将日期转换为纪元,然后再转换回日期,但这仍然无法解决我的问题。在使用moment.js之前,我只是像这样转换了我的纪元时间
方案1:没有moment.js
var val = 62460;
var selected_time = new Date(val * 1000);
console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), ':', selectedTime.getUTCMinutes(), 'in UTC');
我的控制台日志如下所示:选定的时期是:62460,时间是17:21(UTC)
场景2:使用moment.js
var val = 62460;
var selected_time = moment(val).format('hh:mm:ss');
console.log('Selected epoch is : ', val, 'and the time is ', selected_time);
我的控制台日志如下所示:选定的纪元是:62460,时间是UTC,时间是08:01:02
正确的时间是17:21:00。
谢谢。
在第一种情况下,您乘以1000,而在第二种情况下,您忘记了。
var val = 62460
var selected_time = moment(val*1000).utc().format('hh:mm:ss');
同样,默认情况下,MomentJS在本地时间进行解析。因此,您需要将其显式转换为UTC。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句