Home > front end >  Transform ISO string to Date object javascript without format to local date
Transform ISO string to Date object javascript without format to local date

Time:02-03

I'm trying to convert the string 2022-02-01T13:36:57 00:00 to a Date object javascript that returns me Tue Feb 01 2022 13:36:57 without considering the timezone.

But everytime that I try to convert the date it returns: Tue Feb 01 2022 10:36:57 GMT-0300 (Brasilia Standard Time)

I already tried with moment: let now = moment("2022-02-01T13:36:57 00:00").toDate();

with Date: let now = new Date("2022-02-01T13:36:57 00:00");

with UTC too: new Date(Date.UTC(2022, 02, 01, 10, 36, 57))

But all of them returns me the local date (Brasilia Standard Time)

So, the question is:

How can I convert this string 2022-02-01T13:36:57 00:00 to a Date object that keeps the same day, hour, etc ?

CodePudding user response:

You can instantiate your own Intl.DateTimeFormat formatter, or call Date.prototype.toLocaleString().

const
  dateString = '2022-02-01T13:36:57 00:00',
  date = new Date(dateString),
  dateFormatter = new Intl.DateTimeFormat('en-US', {
    timeZone: 'UTC',
    weekday: 'short',
    year: 'numeric',
    month: 'short',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    timeZoneName: 'long',
    hour12: false
  });

console.log(`Local date : ${date}`);
console.log(`  UTC date : ${dateFormatter.format(date).replace(/,/g, '')}`);

CodePudding user response:

The timestamp "2022-02-01T13:36:57 00:00" represents a unique moment in time. If parsed to a Date object, it will create a Date instance with a time value of 1643722617000, which is the offset in milliseconds from the ECMAScript epoch of 1 Jan 1970.

The time value produced from the timestamp is unaffected by local settings as:

  1. It conforms to one of the formats supported by ECMA-262 and therefore parsing is specified by the standard
  2. Contains a fixed offset

The default toString method produces a timestamp for the equivalent date and time in the timezone of the host system, typically called the local date and time. It will produce a different date and time for each host with a different offset, but they will all represent exactly the same moment in time.

E.g.

let timestamp = '2022-02-01T13:36:57 00:00';
let date = new Date(timestamp);
['UTC','America/Sao_Paulo','Asia/Kolkata'].forEach(
  loc => console.log(`${date.toLocaleString('default',{timeZone:loc})} - ${loc}`)
)

If you want the timestamp to be parsed as local (and that should only be done if you know what you are doing and have a very good reason to do so) then remove the offset and parse the remainder:

let timestamp = '2022-02-01T13:36:57 00:00';
let date = new Date(timestamp.substring(0,19));
console.log(date.toString());

Note that the resulting Date represents a different moment in time for each host with a different offset and each such date instance will have a different time value.

  •  Tags:  
  • Related