猫鼬日期格式 [英] Mongoose date format

查看:74
本文介绍了猫鼬日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
});

默认情况下使用"mm.dd.yyyy",因此我拥有的所有格式为"dd.mm.yyyy"的数据都默认为Date.now.

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

有人知道我可以直接在模式中添加格式:"dd.mm.yyyy"函数吗?还有其他想法吗(真的不想更新所有数据)

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)

谢谢您的答复

推荐答案

据我所知,猫鼬没有默认格式".相反,它将Date实例另存为(我认为)RFC 822时间戳(Mon Jan 02 2012 00:00:00 GMT+0100 (CET)),并通过运行new Date(INPUT)从数据库解析它们.

As far as I know, Mongoose doesn't have a 'default format'. Instead, it saves Date instances 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).

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

That last action is your problem:

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

如您所见,JavaScript本身将其解析为mm.dd.yyyy.我不知道是否可以解决而不必更新数据库.

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.

这篇关于猫鼬日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆