【问题标题】:Moment.js: Why this conversion Date -> Unix is giving me 5:00:00 instead of 0:00:00?Moment.js:为什么这个转换 Date -> Unix 给我 5:00:00 而不是 0:00:00?
【发布时间】:2020-12-06 02:37:19
【问题描述】:

我有这个 jquery/moment.js 代码:

var check_in_proper = '15-09-2020';
var ts = moment(check_in_proper, "DD/MM/YYYY").valueOf()/1000;
alert(ts);

这给了我这个时间戳:1600146000,即 2020 年 9 月 15 日,星期二 5:00:00。 但我预计会得到 1600128000,即 2020 年 9 月 15 日星期二 0:00:00。

我做错了什么?

【问题讨论】:

    标签: jquery date momentjs


    【解决方案1】:

    您需要使用utc() 函数来获取正确的时间和日期,而不是使用valueOf 您还可以使用unix()unix() 函数momentJS

    现场演示:

    var check_in_proper = '15-09-2020'; //date
    var ts = moment.utc(check_in_proper, "DD/MM/YYYY").unix() //unix
    var formattedDate = moment.unix(ts).utc().format("dddd, D MMMM YYYY, HH:mm:ss");
    console.log(ts); //1600128000
    console.log(formattedDate); //Tuesday, 15 September 2020, 00:00:00
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      相关资源
      最近更新 更多