本文介绍了猫鼬日期字段 - 将默认设置为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
这篇关于猫鼬日期字段 - 将默认设置为date.now + N天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!