node.js 猫鼬日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16664896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:33:25  来源:igfitidea点击:

Mongoose date format

node.jsmongodbmongoose

提问by Are

Currently having an issue retrieving dates from mongoose. This is my schema:

目前在从猫鼬检索日期时遇到问题。这是我的架构:

var ActivitySchema = new Schema({
    activityName      : String
  , acitivtyParticipant  : String
  , activityType  : String
  , activityDate      : { type: Date, default: Date.now }
  , activityPoint  : Number
});

This defaults to use "mm.dd.yyyy", so all data I have which is in format "dd.mm.yyyy" defaults to Date.now.

这默认使用“mm.dd.yyyy”,所以我拥有的所有格式为“dd.mm.yyyy”的数据默认为Date.now。

Does anyone know if there is a "format: "dd.mm.yyyy" function which I can put directly in the Schema? Any other ideas? (would really not like to update all the data)

有谁知道是否有“格式:”dd.mm.yyyy”函数可以直接放入架构中?还有其他想法吗?(真的不想更新所有数据)

Thank you for any replies

感谢您的任何回复

回答by robertklep

As far as I know, Mongoose doesn't have a 'default format'. Instead, it saves Dateinstances as (I think) RFC 822 timestamps (Mon Jan 02 2012 00:00:00 GMT+0100 (CET)), and parses them back from the database by running new Date(INPUT).

据我所知,猫鼬没有“默认格式”。相反,它将Date实例保存为(我认为)RFC 822 时间戳(Mon Jan 02 2012 00:00:00 GMT+0100 (CET)),并通过运行new Date(INPUT).

That last action is your problem:

最后一个动作是你的问题:

> new Date('01.02.2012')
Mon Jan 02 2012 00:00:00 GMT+0100 (CET)

As you can see, Javascript itself parses it as mm.dd.yyyy. I don't know if that's solvable without having to update your database.

如您所见,Javascript 本身将其解析为mm.dd.yyyy. 我不知道这是否可以解决而无需更新您的数据库。