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

209
Views
add hours to date not working as expected

I want to add hours to date using this function:

function addHoursToDate(date, hours) {
    return new Date(new Date(date).setHours(date.getHours() + hours));
}

const now = new Date();

console.log('now', now);

console.log('then', addHoursToDate(now, 1));

The weird issue is here on SO we get the correct result but if you use the same code on node.js both logs are the same.

How can I fix this?

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can do it without function

const now = new Date();

console.log('now', now);
now.setHours(now.getHours() + 1)
console.log('then', now);

or with function

function addHoursToDate(date, hours) {
date.setHours(date.getHours() + hours)
    return date;
}
const now = new Date();
console.log('now', now);
console.log('then', addHoursToDate(now, 1));
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!