I want to get the current UTC date in JavaScript, but display it in the local date format (like Date.toLocaleDateString()
does).
I first tried to just get the current UTC Date with Date.toUTCString()
but that doesn't actually print out in the local format.
I then tried using the options configuration in toLocaleDateString()
, but that just printed the local date and not the UTC date in the local format.
e.g. new Date().toLocaleDateString(options = {timeZone: "UTC"})
I then tried formatting using Intl.DateTimeFormat()
, but that just gives the same results as Date.toLocaleDateString()
does.
If there was a way to get the locale format then I'd be happy to use that format to format the UTC Date, but as far as I can tell there is none.
For example, given the new Date("Sat, 30 Mar 2019 00:27:19 GMT")
, In the US, I should print out "3/30/2019", in Europe I should print out "30/3/2019", and so on, for every supported locale.
However, new Date("Sat, 30 Mar 2019 00:27:19 GMT").toLocaleDateString(options = {timeZone: "UTC"})
will print out "3/29/2019" instead.