subtract hours from a Date

 

Date setHours and getHours() Methods

To subtract hours from a Date:

 

 

  1. Call the getHours() method on the Date to get the number of hours.
  2. Subtract the hours.
  3. Pass the result of the subtraction to the setHours() method.
function twoHoursBack(date new Date(), hours=2) {
  date.setHours(date.getHours() - hours);

  return date;
}

Tags:

Share:

Related posts