猫鼬日期字段

编程入门 行业动态 更新时间:2024-10-26 21:23:23
本文介绍了猫鼬日期字段 - 将默认设置为date.now + N天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在mongoose模式中,例如:

In a mongoose schema such as:

var EventSchema = new Schema({ title: { type: String, default: '', trim: true, required: 'Title cannot be blank' }, description: { type: String, default: '', trim: true }, start: { type: Date, default: Date.now, required: 'Must have start date - default value is the created date' }, end: { type: Date, default: Date.now + 7 Days, // Date in one week from now required: 'Must have end date - default value is the created date + 1 week' }, tasks: [{ type: Schema.ObjectId, ref: 'Task' }] });

在结束字段的行上,默认日期应设置为+7天。我可以添加预设钩子并将其设置在那里,但想知道是否有一种方法可以在默认字段中内联。

On the line for the "end" field the default date should set to +7 days. I can add presave hook and set it there, but wondering if theres a way to do this inline in the default field.

推荐答案

您可以将7天转换为毫秒转换为当前日期,如此

You can add 7 days converted to milliseconds to current date like this

default: new Date(+new Date() + 7*24*60*60*1000)

甚至喜欢这个

default: +new Date() + 7*24*60*60*1000

更多推荐

猫鼬日期字段

本文发布于:2023-10-24 02:57:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!