How to add 2 hours to a JavaScript Date object?

JavascriptWeb DevelopmentFront End Technology

To add hours to a JavaScript Date object, use the setHours() method. Under that, get the current hours and 2 hours to it. JavaScript date setHours() method sets the hours for a specified date according to local time.

Example

You can try to run the following code to add 2 hours to date.

 Live Demo

<html>
   <head>
      <title>JavaScript setHours Method</title>
   </head>
   <body>
      <script>
         var dt = new Date();
         dt.setHours( dt.getHours() + 2 );
         document.write( dt );
      </script>
   </body>
</html>

Output

Sat Dec 15 2018 15:49:50 GMT+0530 (India Standard Time)
raja
Published on 13-Feb-2018 08:34:23
Advertisements