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

232
Visualizações
Javascript Date object keeps on returning the default date (Mon Jan 19 1970 19:18:18) when trying to convert unix time

I am trying to use the date function to convert unix time that I am trying to convert to a certain format. The function successfully returns the date in the format requested, but the time is wrong. What am I missing here?

const date = (e) => { 
  const unixTime = 1642698000
  const format = {
    weekday: 'long',
    day: 'numeric', 
    month: "2-digit", 
    year: "numeric"
  }
  return(new Date (unixTime).toLocaleString('en-US', format))
}

Expected output (app.)

Tue Jan 18 2022 17:00:00

The output recieved

Mon Jan 19 1970 19:18:18 GMT-0500 (Eastern Standard Time)

I tried following other exampled but I wasn't able to find any that would resolve this issue. I appreciate your help in advance!

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

0

The Date constructor from Javascript accepts the number of milliseconds as timestamp, not unix time (number of seconds). So, to adjust that, is just multiply the unix time by 1000.

const date = () => { 
  const unixTime = 1642698000 * 1000
  const format = {
    weekday: 'long',
    day: 'numeric', 
    month: "2-digit", 
    year: "numeric"
  }
  return(new Date (unixTime).toLocaleString('en-US', format))
}

console.log(date())
// logs Thursday, 01/20/2022
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