I'm wondering if this is normal, or if I'm missing something in the schema setup or query process:
My app, and mongoose, is correctly storing a date as UTC in mongodb. This is confirmed by viewing the documents via the mongo shell. When I retrieve the documents from mongodb via mongoose the date is now local time.
Is there a way to have mongoose keep the date as UTC when queried?
Mongoose and node.js aren't doing anything to your dates, it's simply that the JavaScript Date
type produces a local time string when you call toString()
on it even though it actually contains the time in UTC.
Explicitly call toUTCString()
on your Date
object if you want a UTC time string.
The timestamps are stored timezone agnostically, as a unix timestamp. This timestamp will work across timezones, and node interprets it using your current timezone. You can retrieve the UTC value from the date object using the getUTC* methods such as get getUTCHours()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With