skip to Main Content

Sorry if its a very basic question but I dont understand the following:

When I format the Date object (no matter what library I used), I get a string.

from this: 2022-11-28T16:55:44.000Z (new Date object)
I get this: 2022-11-28 16:55:44 (or other formats obviously depending how I format it)

Even if I turn it back into an object it, the T and 000Z will never be there anymore. Do I just ignore that (seems like it as any library or date methods are ignoring the T and the string ending when formatting) or do I add it ‘back’ Isnt it a problem if dates stored in my db are different (for later queries etc.)?

2

Answers


  1. The Z indicates UTC (Coordinated Universal Time, also known as Greenwich Meridian Time), dropping that changes the meaning – unless your browser or server lives in the Greenwich time zone and it is winter (no daylight saving time).

    You can convert back and forth between a Date object and a UTC string as follows (my browser lives in the Central European time zone):

    > utc = '2022-11-28T16:55:44.000Z'
    '2022-11-28T16:55:44.000Z'
    > d = new Date(utc)
    Mon Nov 28 2022 17:55:44 GMT+0100 (Central European Standard Time)
    > d.toISOString()
    '2022-11-28T16:55:44.000Z'
    

    Alternatively, you can convert back and forth between a Date object and a formatted string in your browser’s or server’s time zone (the last line shows that my browser’s format differs from yours):

    > formatted = '2022-11-28 17:55:44'
    '2022-11-28 17:55:44'
    > d = new Date(formatted)
    Mon Nov 28 2022 17:55:44 GMT+0100 (Central European Standard Time)
    > d.toLocaleString()
    '11/28/2022, 5:55:44 PM'
    

    But you should not store the Date objects in this format in a database, unless you can guarantee that they are always read and written in the same time zone. For example, if you format a Date object with your browser (in CET) and store it, then someone else who reads it and converts it back to a Date object with their browser in the New Zealand time zone will see a wrong value. Also, dates like 9/11/2022 are ambiguous if the formatting rules are not clear (September 11th or November 9th?).

    That’s why I would prefer UTC strings when storing Date objects and use formatted strings only for outputting them to the user and for parsing user input.

    Login or Signup to reply.
  2. I see it even stronger: You should never store dates as strings, it’s a design flaw. Store always proper Date objects. Here on SO you can find hundreds of questions, where people have problems, because they stored date values as (localized) strings. It is not limited to MongoDB, it applies to any database.

    Date objects in MongoDB are UTC times – always and only! Usually the client application is responsible to display the date/time in local time zone and local format.

    What do you mean by "turn it back", i.e. how do you do it?

    You should not rely on new Date(<string>) without time zone. Some browsers/environments may apply UTC time, others may use current local time zone, see Differences in assumed time zone

    Have a look at 3rd party date libraries, e.g. moment.js, Luxon, or Day.js. Usually they provide better control how to parse strings and time zones.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search