Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

77
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda