0

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 ?

2
  • 1
    You've already done it with new Date("2022-02-01T13:36:57+00:00"). The default toString method displays local values, use toISOString to see UTC (+0) values. Commented Feb 1, 2022 at 14:12
  • It is respecting Time Zone, you are just printing the local version, because that is the default behavior. Commented Feb 1, 2022 at 14:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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, '')}`);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.