The first answer of this question suggests that mongoose would adapt the date according to server timezone when retrieving data.

However I don't have this comportement. I set the (node) server timezone with : process.env.TZ='Europe/Paris'

For exemple if I create a simple model like :

const mongoose = require("mongoose");
const testSchema = new mongoose.Schema({
    myDate: { type: Date, required: true },
}, { timestamps: true });

exports.Comment = mongoose.default.model('TestSchema', testSchema); 

But if I create a date with 2020-01-01 20:20:20, when doing TestSchema.find() the date will be : 2020-01-01T19:20:20.000Z so there is two things that I don't understand :

  • Europe/Paris is actually UTC +2, so I would expect the date to be either 2020-01-01T18:20:20.000Z in UTC or 2020-01-01T20:20:20.000Z with the server timezone
  • How to have mongoose automatically set the date to the correct timezone ?

I know that myDate is a Date object, so I can convert it manually but I'd rather not have to do it myself for simple reasons like forgetting to convert one of the date in the application or not having to do it everytime a Date field is added

An easy solution that I can think of would be to register a global plugin for mongoose which would use schema.set('toJSON', ... and schema.set('toObject', ...) with the transform method so i can loop through shema fields and if the field is a Date, update it to my timezone. But I see two problems with this approch :

  • It doesn't sounds very good performance wise if I am querying a lot of documents each with a lot of fields
  • As you can see here I am currently not able to register global plugins...

What would be the best method to get the date in the server timezone format ? I would rather still store them in UTC but setting the hour according to the server timezone.

EDIT :

I just saw that while console.log(myDate) outputs 2018-01-01T19:20:20.000Z console.log(myDate.toString() outputs Mon Jan 01 2018 20:20:20 GMT+0100 (Central European Standard Time) so it seems likes this could be used, even tho I'd rather still have a Date object and converting it to string just before sending it to client (would need some formatting tho since this format is not very user friendly). But then again, how would I do this globally and not for every date

1 Answers

1
Matt Johnson On Best Solutions

A few things:

  • Europe/Paris at 2020-01-01T20:20:20 is UTC+1. It doesn't switch to UTC+2 until Summer Time kicks in on March 29th. Reference here. Thus the conversion to 2020-01-01T19:20:20Z is correct.

  • The output of console.log when passed a Date object is implementation specific. Some implementations will emit the output of .toString() (which is in local time in RFC 2822 format), and some will emit the output of .toISOString() (which is in UTC in ISO 8601 extended format). That is why you see the difference.

  • In general, it is not good to send a local time without also sending a time zone offset. ISO 8601 format is ideal, but you should send either 2020-01-01T19:20:20Z, or 2020-01-01T20:20:20+01:00. Don't just send the date and time without an offset to the client. Otherwise, if your client could be in a different time zone then they would interpret the value incorrectly.

  • Keep in mind that Date objects are not time zone aware. They contain only a Unix timestamp internally, and they convert only to the system's local time zone for the functions that work in local time. They cannot work in any other time zone.

  • Relying on the system local time zone is bad for portability. One doesn't always have the ability to change it, and it doesn't do well when you have to work in multiple time zones. It would be better to not rely on setting a local time zone from Node's TZ variable. Instead, consider writing your code to be independent of any local time zone setting.

  • A time zone aware date library can help with most of your concerns. I can recommend Luxon, js-Joda, Moment + Moment-Timezone, or date-fns + date-fns-timezone.

  • "how would I do this globally" is something I'm not following in your question. Try the approach I described, and if you still have issues then open a new question. Try to be specific and ask a single question. You're likely to get better results that way. Please read How do I ask a good question? and How to create a Minimal, Complete, and Verifiable example. Thanks.