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

154
Vistas
Why converting string dates with yyyy-mm-dd get strange results, but string dates with yyyy/mm/dd get correct ones?

In short, I have the string date 2022-04-01 (this is the startDate.value), and I need to convert it to a Date.

I have this code:

const startDate0Hours = new Date(new Date(startDate.value).setHours(0, 0, 0, 0));
console.log('this is the date (string):', startDate.value);
console.log('this is without replace:', new Date(startDate.value));
console.log('this is with replace (/-/g, "\/"):', new Date((startDate.value).replace(/-/g,'\/')));
console.log('this is with setHours(0,0,0,0):', startDate0Hours)

Which prints this: [screenshot][1]

this is the date (string): 2022-04-01
this is without replace: Thu Mar 31 2022 21:00:00 GMT-0300 (Brasilia Standard Time)
this is with replace (/-/g, "/"): Fri Apr 01 2022 00:00:00 GMT-0300 (Brasilia Standard Time)
this is with setHours(0,0,0,0): Thu Mar 31 2022 00:00:00 GMT-0300 (Brasilia Standard Time)

Why does string date with '-' gets converted a day before, and 21h, but string date with '/' get correctly converted?

about 4 years ago · Santiago Trujillo
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