如何只用猫鼬保存日期部分,而不用ISODate格式保存 [英] How to save only date part with mongoose, not in ISODate format

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

问题描述

我怎么只用猫鼬schmea保存日期部分.目前我的模型是这样的:

How do I save only date part with mongoose schmea. Currently my model is like this:

myDate: { type: Date}

哪怕我只通过了"2016-02-27",也会将日期另存为"2016-02-27T00:00:00.000Z".

Which saves date as "2016-02-27T00:00:00.000Z" even if I pass only: "2016-02-27".

推荐答案

我正在使用:

dateOfBirth: { type: Object }

并保存如下:

dateOfBirth: {year: 1988, month: 0, day: 8}

这给了我

  1. 通过yearmonth
  2. 进行搜索的能力
  3. 创建日期对象的能力:

  1. ability to search by year, month
  2. ability to make date object:

const dateOfBirth = new Date(
  user.dateOfBirth.year, 
  user.dateOfBirth.month, 
  user.dateOfBirth.day
);

这将避免时区偏移,因为date对象将在设备的环境中创建

which will avoid timezone shiftings, since date object will be created on device's environment

这篇关于如何只用猫鼬保存日期部分,而不用ISODate格式保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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