How to add hours to a date in TypeScript

1 Answer

0 votes
function addHoursToDate(hours : number, date = new Date()) : Date {
    date.setTime(date.getTime() + hours * 60 * 60 * 1000);
 
    return date;
}
 
const date = new Date('2022-03-13T09:51:22');
 
console.log(addHoursToDate(6, date).toString());
 
 
   
   
   
   
/*
run:
   
"Sun Mar 13 2022 15:51:22 GMT+0200 (Israel Standard Time)" 
   
*/

 



answered Mar 21, 2022 by avibootz
edited Mar 21, 2022 by avibootz
...