JavaScript add days to date | Simple example code

JavaScript Doesn’t have a built-in function to add a button but you can create it. In function Add days to a new instance of JavaScript Date.

Example add days to date in JavaScript

HTML example code where we create function. A function takes 2 inputs first for the date and the second is for the day.

This takes care of automatically incrementing the month if necessary.

<!DOCTYPE html> 
<html>  
<body> 

    <script> 
        function addDays(date, days) {
          var result = new Date(date);
          result.setDate(result.getDate() + days);
          return result;
      }
      console.log(addDays('2020-01-30',1));
  </script> 

</body> 

</html>

Output:

JavaScript add days to date example

Q: How to add days to the current date in JavaScript?

Answer: Same as above example, remove date parameter and use current date.

<!DOCTYPE html> 
<html>  
<body> 

    <script> 
        function addDays(days) {
          var result = new Date();
          result.setDate(result.getDate() + days);
          return result;
      }
      console.log(addDays(1));
  </script> 

</body> 

</html> 

Output:

add days to the current date in JavaScript

Do comment if you have any doubts and suggestions on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.