gpt4 book ai didi

javascript - 在 MongoJS 中检索日期

转载 作者:可可西里 更新时间:2023-11-01 10:44:19 25 4
gpt4 key购买 nike

我正在尝试在 mongojs 中存储和检索日期。这是它的保存方式:

var game = { startedOn: new Date() };
db.games.save(game);

当我获取它时,日期不再是日期。这是约会的某种包装。 _d 字段似乎是一个日期,但奇怪的是我必须以这种方式访问​​它。

db.games.find(function(err, games){
console.log(game[0].startedOn);
});

这个日志:

{ _useUTC: true,
_isUTC: true,
_l: null,
_i: null,
_f: null,
_d: Sun Jun 09 2013 21:49:26 GMT-0500 (CDT) }

在 mongo-js 中存储/检索日期的正确方法是什么?

最佳答案

DateTimeDate 值在存储到 MongoDB 之前应转换为 UTC 时间

您上面的方法在 nodejs 中运行良好。

以下是您如何检索它:

new Date(parseInt(this.toString().slice(0,8), 16)*1000);

更多info

关于javascript - 在 MongoJS 中检索日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17016450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com