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

How to add months to a date in JavaScript?


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>