-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
moment.utc drops time part when used default #11233
Comments
Opened ticket to moment as well. |
Just a guess:
So this schema definition might work for you: const kittySchema = new mongoose.Schema({
birthday: {default: () => moment.utc(), type: Date}
}); |
@Maximusya Maybe there is a conversion issue. Probably it does a .toString, of the Moment-Object, which results in loss of milliseconds.Then it should be actually birthday: {default: () => moment.utc().toDate(), type: Date} |
What I meant is here: https://jsfiddle.net/n3vj01ax/: console.log(moment.utc().toString());
console.log(moment.utc({ foo: 1 }).toString());
|
As the comments have pointed out, you need to change const kittySchema = new mongoose.Schema({
birthday: { default: moment.utc, type: Date }
});
// birthday: 2022-01-18T00:00:00.000Z
const kittySchema = new mongoose.Schema({
birthday: { default: () => moment.utc(), type: Date }
});
// birthday: 2022-01-18T12:41:43.889Z |
Thanks all for the help! |
docs(migration): add note to change default functions to schema
Describe the bug
When we create a date field and default it to moment.utc, when it gets populated the date part of the timestamp is correct, but the time part gets lost and defaulted to midnight.
To Reproduce
Steps to reproduce the behavior:
Run the attached script.
Expected behavior
Timestamp to be set to now with hour, minute and second correctly displayed (not zero).
Got this:
Expected:
Desktop:
Moment-specific environment
Mon 17 Jan 2022 10:32:37 GMT
Please run the following code in your environment and include the output:
The text was updated successfully, but these errors were encountered: