console.log("Timestamp: " + data[0].timestamp)
>时间戳:1419860543788
var last_date = new Date(data[0].timestamp);
console.log(util.inspect(last_date));
> 无效号码
如果我像这样创建日期对象
var last_date = new Date(1419860543788);
它工作得很好,那么这里有什么?知道吗?我应该提到,数据是从猫鼬查找方法返回的 JSON 对象(数据)
Blog.find({}).sort({timestamp: -1}).skip(0).limit(10).exec(function (err, data) {
可能data[0].timestamp
是一个String
,如果将其转换为Number
则可以将其转换为Date
实例:
var data = [{timestamp: '1419860543788'}], log = Helpers.log2Screen;
log(new Date(data[0].timestamp));
log(new Date(+data[0].timestamp)); //<= using + operator to convert to Number
<script src="http://kooiinc.github.io/JSHelpers/Helpers-min.js"></script>
根据您的评论,data[0].timestamp
的值是一个字符串。
new Date(value);
Parameters
value
Integer value representing the number of milliseconds
since 1 January 1970 00:00:00 UTC (Unix Epoch).
现在,由于您传递的是一个字符串,该字符串不是整数/数字,它将执行以下操作:
new Date(dateString);
Parameters
dateString
String value representing a date. The string should
be in a format recognized by the Date.parse() method
(IETF-compliant RFC 2822 timestamps and also a version
of ISO8601).
Date.parse()
理解"星期三,01 Jan 20xx 00:00:00 GMT"和 ISO 日期等日期,但不能理解时间戳。
如果你要做:
new Date(parseInt(data[0].timestamp, 10));
您将获得一个有效的Date
对象。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date