Comment 2 for bug 1917396

Revision history for this message
Josh Stompro (u-launchpad-stompro-org) wrote :

I don't really get why javascript date doesn't have a direct way to show the local date in ISO format?

There seem to be various methods to do it. Is there a standard way to do this in Evergreen already?

Here are a couple that seem to be the simplest.

const event = new Date('Tue Mar 02 2021 20:55:38 GMT-0600 (Central Standard Time)');

//Use the fact that Sweden uses a format that is very close to ISO
//Do locale formats change over time? That may be the downside to this method.
console.log(event.toLocaleString('sv').replace(' ', 'T'));

//Shift the date by the timezone offset, so when it is converted to UTC it is actually the local time
//remove the Z postfix for completeness
var tzoffset = event.getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(event - tzoffset)).toISOString().slice(0, -1);
// => '2015-01-26T06:40:36.181'
console.log(localISOTime);

//one line
var isoDateTime = new Date(event.getTime() - (event.getTimezoneOffset() * 60000)).toISOString();
console.log(isoDateTime);