0

I will begin using MongoDB in a project and made the following observation: A ‘Date’, e.g.: ISODate("2000-07-24T00:00:00.000Z") is stored in as UTC 0 (or Zulu).

Let’s assume that an end user submits a plain date, such as “2000-07-24” and his or her local timezone is -5.

Because of the ‘Z’ character in the time (indicates UTC 0), storing the following would be incorrect because the user’s 5 hour difference:

ISODate("2000-07-24T00:00:00.000Z")

I am assuming that it would be more proper to consider the end user’s local time zone into account and store the date as follows:

ISODate(“2000-07-23T19:00:00.000Z”)

By doing this, I can see that any queries would need a timezone adjustment for 'Date'.

By storing all ‘Date’ fields as UTC 0, we can guarantee that exact points in time from people posting from different continents is properly handled.

Does this seem like a good way to handle timezones in MongoDB?

Caroline Beltran
  • 888
  • 2
  • 9
  • 22
  • 1
    Yes, it does .. – Alex Blex Oct 07 '19 at 16:54
  • @AlexBlex, I'm a little nervous but I'm going to go for it. I do appreciate your comment, it gives me some confidence that I am looking at this topic correctly. – Caroline Beltran Oct 07 '19 at 16:59
  • 1
    It's a bit too broad for stackoverflow format yet there are few similar questions https://stackoverflow.com/questions/11537106/is-it-always-a-good-idea-to-store-time-in-utc-or-is-this-the-case-where-storing please read all answers including linked questions. It should give you a bit more confidence. The only flaw of converting local to UTC is you cannot do it reliably for future dates but it makes calculation so much cheaper that the risk of the error is acceptable in most cases. – Alex Blex Oct 07 '19 at 17:12
  • My mistake. I deleted the comment. – Adam Harrison Oct 07 '19 at 22:35

0 Answers0