moment.utc drops time part when used default
See original GitHub issueDescribe 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.
const mongoose = require('mongoose');
const moment = require('moment');
main().catch(err => console.log(err)).then();
async function main() {
await mongoose.connect('mongodb://localhost:27017/test');
const kittySchema = new mongoose.Schema({
birthday: {default: moment.utc, type: Date}
});
const Kitten = mongoose.model('Kitten', kittySchema);
const cat = new Kitten({});
console.log(cat);
}
Expected behavior Timestamp to be set to now with hour, minute and second correctly displayed (not zero). Got this:
Erikas-Macbook:BUG erikaarnocki$ ts-node bug.ts
{
_id: new ObjectId("61e5471c712520a0f39ebbef"),
birthday: 2022-01-17T00:00:00.000Z
}
Expected:
Erikas-Macbook:BUG erikaarnocki$ ts-node bug.ts
{
_id: new ObjectId("61e5471c712520a0f39ebbef"),
birthday: 2022-01-17T10:39:40.000Z
}
Desktop:
- ts-node v8.10.2
Moment-specific environment
- The time zone setting of the machine the code is running on
Erikas-Macbook:BUG erikaarnocki$ locale
LANG="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_CTYPE="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_ALL=
- The time and date at which the code was run
Mon 17 Jan 2022 10:32:37 GMT
- Other libraries in use (TypeScript, Immutable.js, etc)
{
"devDependencies": {
"@types/node": "^17.0.9",
"cypress-wait-until": "^1.7.1"
},
"dependencies": {
"moment": "^2.29.1",
"mongoose": "^6.1.6",
"typescript": "^4.5.4"
}
}
Please run the following code in your environment and include the output:
console.log((new Date()).toString())
console.log((new Date()).toLocaleString())
console.log((new Date()).getTimezoneOffset())
console.log(navigator.userAgent)
console.log(moment.version)
Erikas-Macbook:BUG erikaarnocki$ ts-node
> console.log((new Date()).toString())
Mon Jan 17 2022 10:33:56 GMT+0000 (Greenwich Mean Time)
undefined
> console.log((new Date()).toLocaleString())
17/01/2022, 10:33:56
undefined
> console.log((new Date()).getTimezoneOffset())
0
undefined
> console.log(navigator.userAgent)
/Users/erikaarnocki/BUG/[eval].ts:1
console.log(navigator.userAgent);
^
Uncaught ReferenceError: navigator is not defined
at /Users/erikaarnocki/BUG/[eval].ts:1:13
at Script.runInThisContext (vm.js:133:18)
at exec (/usr/local/lib/node_modules/ts-node/src/bin.ts:347:17)
at /usr/local/lib/node_modules/ts-node/src/bin.ts:337:27
at Array.reduce (<anonymous>)
at _eval (/usr/local/lib/node_modules/ts-node/src/bin.ts:336:18)
at REPLServer.replEval (/usr/local/lib/node_modules/ts-node/src/bin.ts:387:16)
at bound (domain.js:413:15)
at REPLServer.runBound [as eval] (domain.js:424:12)
at REPLServer.onLine (repl.js:817:10)
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
moment.utc drops time part when used as mongoose default
When we create a date field and default it to moment.utc, when it gets populated the date part of the timestamp is correct,...
Read more >Moment Timezone | Docs
To reset the default time zone to local, use moment.tz.setDefault with no arguments. moment.tz.setDefault();. This is a global setting (shared by all modules) ......
Read more >javascript - moment.js - UTC gives wrong date - Stack Overflow
By default, MomentJS parses in local time. If only a date string (with no time) is provided, the time defaults to midnight.
Read more >UTC - momentjs.com
By default, moment parses and displays in local time. If you want to parse or display a moment in UTC, you can use...
Read more >User friendly date-time parsing functions - lubridate
parse_date_time() parses an input vector into POSIXct date-time object. ... train = TRUE, drop = FALSE ) parse_date_time2( x, orders, tz = "UTC", ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just a guess:
default: moment.utc
in your schema: that’s a function accepting somedoc
object (as per https://mongoosejs.com/docs/migrating_to_6.html#document-parameter-to-default-functions)moment.utc(obj)
expects year, month etc fields in an object argument (per https://momentjs.com/docs/#/parsing/object/).Omitted units default to 0 or the current date, month, and year.
So this schema definition might work for you:
Thanks all for the help! This worked perfectly:
birthday: {default: (): Date => moment.utc().toDate(), type: Date}