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

316
Visualizações
JavaScript returns incorrect date value after using setHours()

This is pretty self explanatory if you compare the two date values that are returned. If the second value is also a date, but for some reason printed in a strange way, can I somehow convert it to similar format to the first value, so I know that the time is set correctly?

const date = new Date()
console.log(date)
console.log(date.setHours(5))

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

0

First you have to set the hour then you can log the date with correct format. Because setHours() is a function that returns the number of milliseconds and the updated date. The default date will be formatted according to universal” UTC time. IST will be 6hrs behind hence if you will setHours(12) then it will be 6pm in IST and if you will setHours(n) then it will be n+6 in 24hrs format.

let date = new Date()
console.log(date)
date.setHours(5)
console.log(date)

//OR

date = new Date(date.setHours(5))
console.log(date)

about 4 years ago · Juan Pablo Isaza Relatório

0

var date = new Date()
console.log(date)
date = new Date(date.setHours(5))
console.log(date)

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