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

137
Vistas
How to ONLY replace the next two characters

I am trying to reset a ISO time to local time, but I am having some trouble converting it.

Currently, I:

// incoming ISO date
let date = new Date('2021-11-08T20:31:23.746Z');

// 1. get timezone offset in hours
const offset = date.getTimezoneOffset() / 60;

// 2. calculate local hour
let localHour = date.toISOString().split('T')[1].substring(0, 2) - offset;

// 3. calculate local time
let localTime = date.toISOString().replace(/[T-:]/, localHour);

console.log(localTime);

// ISO Date
// 2021-11-08T20:31:23.746Z (original)
// 2021-11-08T13:23.746Z (local timezone)

In step 3, I am trying to only replace the first two characters after 'T', however, this is not the correct solution with Regex.

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

0

The Date constructor automatically converts the date to your locale after parsing.

// incoming ISO date
let date = new Date('2021-11-08T20:31:23.746Z');

const res = date.toLocaleString().replaceAll('/', '-').split(', ').join('T')
console.log(res)

// ISO Date
// 2021-11-08T20:31:23.746Z (original)
// 2021-11-08T13:23.746Z (local timezone)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Stop right there and don't do this. Date management is hard. You are going to shoot yourself in the foot. Use a library, for example dayjs.

dayjs(date).local().toDate()
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