Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
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 answers
Answer question

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 Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!