【问题标题】:Invalid date when I put a future timestamp [duplicate]我放置未来时间戳时的日期无效[重复]
【发布时间】:2017-11-27 09:30:57
【问题描述】:

假设有一个未来的时间戳日期。

var timestamp="1511858535000" //Future timestamp
var date=new Date(timestamp);
console.log(JSON.stringify(date));// this line give me Invalid Data

谁能帮我理解为什么?

【问题讨论】:

  • 您使用的是什么“未来时间戳”?可以举个例子吗?
  • @dferenc 例如,这个时间戳“1511858535000”是 2017 年 11 月 28 日星期二 08:42:15。当我将此时间戳放在 var date=new Date(timestamp) 中时,它给了我“无效日期”
  • new Date(); 的时间戳无效。
  • @dferenc 为什么时间戳无效?因为是未来的时间戳?
  • 为了将来参考,最好提供完整的代码来演示问题(例如,在问题中包含时间戳而不是.... //Future timestamp

标签: javascript date


【解决方案1】:

您在 cmets 中提到的时间戳可以正常工作,但您必须将其作为数字而不是字符串传递给 Date 构造函数:

var timestamp = "1511858535000";
var date = new Date(Number(timestamp));
console.log(JSON.stringify(date));

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 2011-09-07
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多