How to add months to a date in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To add months to a JavaScript Date, use the setMonth() method. JavaScript date setMonth() method sets the months for a specified date according to local time.

Example

You can try to run the following code to add 2 months.

<html>
   <head>
      <title>JavaScript setMonth Method</title>
   </head>
   <body>
      <script>
         var dt = new Date("August 20, 2017 11:30:25");
         dt.setMonth( dt.getMonth() + 2 );
         document.write( dt );
      </script>
   </body>
</html>
raja
Published on 13-Feb-2018 09:08:09
Advertisements