Others



How to add number of days to Date in JavaScript


In this example, we take the current date and the number of days from user input and add a number of days to the given date. Finally, display the resulting formatted date.

The following example shows, how to add number of days to Date in JavaScript

Example
<html>
  <body>
    <p> Current Date <input type='text' value='2023-08-01' id='current_date'> </p>
    <p> No of Days to Add <input type='text' value='20' id='no_of_days'> </p>
    <p> <input type='button' value='New Date' onclick='addDays()'> </p>
    <p id='new_date'></p>
    
    <script>
    function addDays() {
    
      //Current Date
      let current_date=document.querySelector("#current_date").value;
      
      //No of Days to Add
      let no_of_days=Number(document.querySelector("#no_of_days").value);

      let result = new Date(current_date);
      result.setDate(result.getDate() + no_of_days);
      
      //Change date Format to dd-mm-yyyy
      let year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(result);
      let month = new Intl.DateTimeFormat('en', { month: '2-digit' }).format(result);
      let day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(result);
      let new_date=`${year}-${month}-${day}`;

      document.querySelector("#new_date").innerText=new_date;
    }    
    </script>
  </body>
</html>

Try it Yourself

Demo