Computer >> Computer tutorials >  >> Programming >> Javascript

How to add 2 hours to a JavaScript Date object?


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.

<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)