Skip to content
Related Articles

Related Articles

Improve Article

How to get tomorrow’s date in a string format in JavaScript ?

  • Last Updated : 13 Apr, 2021

In this article, we will see how to print tomorrow’s date in string representation using JavaScript.

To achieve this, we use the Date object and create an instance of it. After that by using the setDate() method, we increase one date to the present date. Now by using the getDate() method you will get tomorrow’s date. Now to convert that date to the string we use the string template literals, getFullYear, getMonth, padStart methods. Below is the implementation of this:

Example: 

Javascript




<script>
    const tomorrow = () => {
  
        // Creating the date instance
        let d = new Date();
  
        // Adding one date to the present date
        d.setDate(d.getDate() + 1);
  
        let year = d.getFullYear()
        let month = String(d.getMonth() + 1)
        let day = String(d.getDate())
  
        // Adding leading 0 if the day or month
        // is one digit value
        month = month.length == 1 ? 
            month.padStart('2', '0') : month;
  
        day = day.length == 1 ? 
            day.padStart('2', '0') : day;
  
        // Printing the present date
        console.log(`${year}-${month}-${day}`);
    }
      
    tomorrow()
</script>

Output:

"2021-03-28"

Example 2:

If the date is given :

Javascript




<script>
    const tomorrow = (dt) => {
  
        // Creating the date instance
        let d = new Date(dt);
  
        // Adding one date to the present date
        d.setDate(d.getDate() + 1);
  
        let year = d.getFullYear()
        let month = String(d.getMonth() + 1)
        let day = String(d.getDate())
  
        // Adding leading 0 if the day or month
        // is one digit value
        month = month.length == 1 ? 
            month.padStart('2', '0') : month;
  
        day = day.length == 1 ? 
            day.padStart('2', '0') : day;
  
        // Printing the present date
        console.log(`${year}-${month}-${day}`);
    }
  
    tomorrow("2020-12-31")
    tomorrow("2021-02-28")
    tomorrow("2021-4-30")
</script>

Output:

"2021-01-01"
"2021-03-01"
"2021-05-01"

Note: Enter the date in yyyy-mm-dd format.




My Personal Notes arrow_drop_up
Recommended Articles
Page :