Skip to content Skip to sidebar Skip to footer

How To Subtract 2 Hours From User's Local Time?

Can anyone give me a simple JavaScript code block that will allow me to display the local time minus 2 hours?

Solution 1:

Subtract from another date object

var d = newDate();

d.setHours(d.getHours() - 2);

Solution 2:

According to Javascript Date Documentation, you can easily do this way:

var twoHoursBefore = newDate();
twoHoursBefore.setHours(twoHoursBefore.getHours() - 2);

And don't worry about if hours you set will be out of 0..23 range. Date() object will update the date accordingly.

Post a Comment for "How To Subtract 2 Hours From User's Local Time?"