Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
Transform ISO string to Date object javascript without format to local date

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 ?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda