【问题标题】:Getting incorrect dates converting timestamp with new Date使用新日期获取不正确的日期转换时间戳
【发布时间】:2017-07-28 18:01:24
【问题描述】:

我正在尝试将时间戳转换为日期,但我得到的日期不正确。我正在使用 Angular 和 Typescript 开发一个项目。

我的时间戳如下: 1451642400(2016 年 1 月 1 日)和 1454320800(2016 年 2 月 1 日)

如果我编码:

date = new Date(1451642400);
console.log(date.toLocaleString());
date = new Date(1454320800);
console.log(date.toLocaleString());

我得到: 17/1/1970 20:14:02 和 17/1/1970 20:58:40

什么时候我应该得到: 2016 年 1 月 1 日 10:00:00 和 2016 年 1 月 2 日 10:00:00

有什么问题?我该如何解决?

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    您需要将 Unix 时间戳乘以 1000,以毫秒为单位。 如果你这样做,它会起作用:

        date = new Date(1451642400 * 1000);
        console.log(date.toLocaleString());
        date = new Date(1454320800 * 1000);
        console.log(date.toLocaleString());

    【讨论】:

      【解决方案2】:

      新的 Date() 的参数以毫秒为单位。所以尝试使用 1000 的时间戳以在毫秒内完成。

      var date = new Date(1451642400 * 1000);

      【讨论】:

        猜你喜欢
        • 2019-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2016-11-26
        • 2014-05-23
        • 2023-03-16
        相关资源
        最近更新 更多