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

399
Visualizações
how to solve Invalid Date {} JavaScript

when I try to get the year, the month and the time of a date, I get Invalid Date {}

const d = new Date();
let time = new Date(d.getFullYear(), d.getMonth(), d.getTime());
document.getElementById("demo").innerHTML = time;
<div id='demo'></div>

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

0

new Date().getTime() returns the number of milliseconds that have elapsed since midnight on Jan 1, 1970, while new Date().getDay(), which is what you're looking for, returns the current day.

const d = new Date();
let time = new Date(d.getFullYear(), d.getMonth(), d.getDay());
document.getElementById("demo").innerHTML = time;
<div id='demo'></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

the month argument range from 0 to 11 in javasript, to get the correct month try (d.getMonth()+1).

const d = new Date();

//I don't need utc date I need to ge the date on that form: year, month, time

let time = d.getUTCFullYear() + "," +(d.getMonth()+1) + ","+d.getTime();

console.log("time",time);
about 4 years ago · Juan Pablo Isaza Relatório

0

well here are pair of errors in your code working with the javascript Date object API. d.getFullYear() gives 2022 and this is correct, and d.getMonth() returns the months starting by 0 but as you are showing the object directly you don't need to worry about that, the problem surges when you call d.getTime() that returns:

A number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date.

MDN reference

So maybe what you are trying to do is this instead:

        const d = new Date();           
        let time = new Date(d.getFullYear(), d.getMonth(), d.getUTCDate());
        document.getElementById("demo").innerHTML = time;
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