【问题标题】:toDateString() not working to convert a unix timestamp to human-readable format?toDateString() 无法将 unix 时间戳转换为人类可读的格式?
【发布时间】:2014-05-26 07:51:19
【问题描述】:

我正在这样做:

console.log('before:' + back[i].dateCreated);
back[i].dateCreated = new Date(back[i].dateCreated).toDateString();
console.log('after: ' + back[i].dateCreated);

我得到这样的控制台日志:

before:1397091642
after: Fri Jan 16 1970 

但如果我将那个 epoc 日期插入 this conveter,它就会出现 2014 年 4 月 10 日

那么“toDateString()”是否没有按照我认为的那样做,或者我的时间戳有问题还是什么?

【问题讨论】:

    标签: javascript time epoch


    【解决方案1】:

    JavaScript 时间不是以秒为单位,而是以毫秒为单位。所以你必须乘以 1000:

    var epoc = 1397091642;
    var date = new Date(epoc * 1000);
    var humanReadable = date.toDateString();
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 2010-12-11
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多