by @kodeazy

JavaScript How to subtract n number of hours from local time ?

Home » javascript » JavaScript How to subtract n number of hours from local time ?
  • In this blog we will discuss on how to subtract n number of hours from the date in javascript.
  • To subtract from the number of hours we set the hours by getting the hours from the date object.
  • Below is the syntax for date object.

    date.setHours(date.getHours()-2);
  • Below is the example porgram on subtracting n number of hours from the date object.

    let date=new Date();
    let n=2;
    date.setHours(date.getHours()-n);
    document.querySelector('#header').innerHTML = date
    console.log(date);

    Output:

    Mon Sep 05 2022 15:56:17 GMT+0530 (India Standard Time)