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

271
Visualizações
JavaScript date does not accept a certain date

I have a problem regarding the JavaScript date function.

var atime = new Date("07.08.2021");
var btime = new Date("19.08.2021");
var ctime = new Date("03.10.2021");
var today = new Date();

console.log(atime);
console.log(btime);

The variables "atime", "ctime" and "today" are working perfectly fine, but btime does not. It always says "Invalid Date", but why, it is completely the same as atime, or ctime?

It does not work with new Date("08/19/2021") either?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Refer to documentation of Date at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#several_ways_to_create_a_date_object that warns for parsing strings

Note: Parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

Prefer this kind of notation new Date(2021, 8, 19)

In addition, I would strongly recommend you invest some time to consider using Moment.js, e.g. https://momentjs.com/docs/#/parsing/string-format/

about 4 years ago · Juan Pablo Isaza Relatório

0

As stated here you should only use one of the 3 date formats:

  1. ISO Date: "YYYY-MM-DD" (The International Standard)
  2. Short Date: "MM/DD/YYY"
  3. Long Date: "Mar 25 2015" or "25 Mar 2015"

Other formats might get interpreted different depending on which browser you are using. Switching your date format to Short Date would probably fix your Problem

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