【问题标题】:Convert epoch time with moment.js in javascript在javascript中使用moment.js转换纪元时间
【发布时间】:2016-03-13 04:38:53
【问题描述】:

我已经检查了这个Using momentjs to convert date to epoch then back to date,但它仍然无法解决我的问题。 在我使用 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

谢谢。

【问题讨论】:

    标签: javascript angularjs ionic-framework momentjs epoch


    【解决方案1】:

    在第一种情况下,您乘以 1000,在第二种情况下您忘记了。

    var val = 62460
    var selected_time = moment(val*1000).utc().format('hh:mm:ss');
    

    此外,默认情况下,MomentJS 以本地时间解析。因此,您需要将其显式转换为 UTC。

    【讨论】:

    • 抱歉忘记通知了,其实我已经用1000测试过了,结果显示的是01:21:00。
    猜你喜欢
    • 2013-08-04
    • 2020-09-29
    • 2017-04-06
    • 2019-01-13
    • 2012-01-30
    • 2022-08-04
    • 2016-06-07
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多