How to add hours to a date in JavaScript

1 Answer

0 votes
function addHoursToDate(hours, date = new Date()) {
  	date.setTime(date.getTime() + hours * 60 * 60 * 1000);

  	return date;
}

const date = new Date('2022-03-12T08:52:21');

console.log(addHoursToDate(5, date).toString());


  
  
  
  
/*
run:
  
"Sat Mar 12 2022 13:52:21 GMT+0200 (Israel Standard Time)"
  
*/

 



answered Mar 21, 2022 by avibootz
...