How to subtract hours from a date in TypeScript

1 Answer

0 votes
function subtractHoursFromDate(hours : number, date = new Date()) {
  	date.setHours(date.getHours() - hours);

  	return date;
}

const date = new Date('2022-03-19T18:37:24');

console.log(subtractHoursFromDate(4, date).toString());





/*
run:

"Sat Mar 19 2022 14:37:24 GMT+0200 (Israel Standard Time)" 

*/

 



answered Mar 17, 2022 by avibootz
...