Search code examples
javascriptdatedatetimefirefoxlocale

Formatting a "YYYY-MM-DD" date string according to locale in Firefox


I'm looking at a semi-old codebase where the following code is used to format a date in YYYY-MM-DD format according to a user's locale:

new Date('2000-01-01').toLocaleDateString(navigator.language)

However, this doesn't work in Firefox because new Date('2000-01-01') returns a datetime (time is 00:00) in UTC while toLocaleDateString uses the user's local timezone, so the above will return "December 31, 1999" for a user in the US.

What is the sane, safe way of doing this across browsers? Is it possible to do without one or more extra dependency?


Solution

  • If you add a timestamp to that date string it seems to be initialized with that time in the local timezone:

    new Date('2000-01-01T00:00:00');
    

    I tried this in both Chrome and Firefox and it seems to work as you want. However, creating a date with a string should be avoided as there's no guarantee it works consistently across different browsers. It's better to break the date into its parts, parse it as numeric values and initialize the date that way:

    var dateParts = '2000-01-01'.split('-').map(Number);
    new Date(
      dateParts[0], 
      dateParts[1] - 1, // month is base 0
      dateParts[2]
    );
    

    Update: Turns out Safari assumes UTC even if appending a timestamp to the date string, so this is one more reason to parse it and initialize the date with numeric values, as this always uses the local timezone.