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

72
Vistas
Showing NaN on Converting 12 HR format String to Date in Javascript in Firefox

Let's say I've to convert this string into a Javascript Date object with this line:

new Date('2001-01-31 02:00:00 pm')

In Chrome it is fine. But in Firefox, it shows an Invalid Date. Why is that? I handled it manually, parsed the Hour part and added 12 hours when needed. But, is there any other way to convert this?

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

0

I think issue you are facing is to do with differences between Chromium based browsers and the Gecko based firefox. Thus they handle your string a little differently from each other. As for converting a string into a Date object, the only way I can think of is by parsing it, ie passing it into Date's constructor (as you have done).

Here is how I would solve this issue so it works in all browsers (by using the built-in methods inside the Date object).

const myDate = new Date();
myDate.setFullYear(2001);
myDate.setUTCMonth(0);
myDate.setUTCDate(31);
myDate.setUTCHours(0);
myDate.setUTCMinutes(0);
myDate.setUTCSeconds(0);
myDate.setUTCMilliseconds(0);


console.log(myDate); // 2001-01-31T02:00:00.000Z

// Now add a year:
const oldYear = myDate.getFullYear();
myDate.setFullYear(oldYear + 1);



// Results:
console.log("Expected result: year is 2002");
console.log(myDate); // Year is 2001 + 1 = 2002

Of course in your use case you may want to add hours, months or any other amount of time, so you can adapt accordingly.

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