【问题标题】:How can I store String type DateTime string to ISO DateTime Format in MongoDB in NodeJS?如何在 NodeJS 的 MongoDB 中将 String 类型的 DateTime 字符串存储为 ISO DateTime 格式?
【发布时间】:2021-01-01 00:51:21
【问题描述】:

我从 android 应用程序接收字符串格式的 DateTime Strings 数据。 根据之前开发者的说明,它是 RFC 3339 格式的字符串。

我一直在将此字符串转换为 Node.js 中 MongoDB 的日期格式 详细编辑文档,格式如下。

[我收到的当前字符串 -> MongoDB 中的日期格式,我想存储] [“2020-09-14 08:18:56”,-> ISODate(“2020-09-14T08:42:41.000Z”)]

有什么方法可以通过编程将这些字符串保存到 NodeJS 上的 MongoDB 中?

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    RFC 3339 是 ISO 6081 的配置文件(一组标准派生)(请参阅这些 SO 帖子:[1][2]),因此您只需将字符串传递给 Date 的构造函数并获得有效的jsDate:

    var raw = "2020-09-14 08:18:56";
    var date = new Date(raw);
    console.log(date);

    【讨论】:

    • 我确实添加了几个小时作为对原始时间字符串的更正,并按照您上面提到的方式进行了转换。感谢您的建议 :)
    猜你喜欢
    • 2019-06-02
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多