How to Get Tomorrow's Date in JavaScript

How to Get Tomorrow's Date in JavaScript

Ferenc Almasi β€’ Last updated 2021 September 18 β€’ Read time 4 min read
  • twitter
  • facebook
JavaScript

Using the Date object in JavaScript, we can easily get tomorrow's date, today, or even yesterday. Here are the examples of how to do so:

Copied to clipboard!
const tomorrow  = new Date(); // The Date object returns today's timestamp

tomorrow.setDate(tomorrow.getDate() + 1);
date.js

You just have to get the current date and you can use the setDate method on the Date object to set the day of the date. Then you can increase the date by the number of days you need. In this example, to get tomorrow's date, we can increase it by 1.

The getDate method, on the other hand, gets the day of the month. Using the same logic, we can easily get yesterday as well:

Copied to clipboard!
const yesterday = new Date();

yesterday.setDate(yesterday.getDate() - 1);
date.js

In case we are at the end of the month, and we increase the day by 1, we get the first day of the next month, therefore we don't need to worry about handling exceptions. You can also use this one-liner to get tomorrow's date in JavaScript:

Copied to clipboard!
const getTomorrow = () =>  new Date.setDate(new Date.getDate() + 1);
getTomorrow.js

How to Format Dates

If you would also like to format your dates, you can use the DateTimeFormat object.

Copied to clipboard! Playground
const today = new Date('2020-04-09');

const config = {
  year:  'numeric',
  month: 'short',
  day:   '2-digit'
};

const dateTimeFormat = new Intl.DateTimeFormat('default', config);

// This will return "Apr 09, 2020"
console.log(dateTimeFormat.format(today));
format.js

For the full list of available properties and values that the config object can take, please refer to the Internationalization API Specification.

If you want to have full control over the formatting, you can also use the formatToParts method of the DateTimeFormat object in conjunction with a custom function:

Copied to clipboard! Playground
const today = new Date('2020-04-09');

const config = {
  year:  'numeric',
  month: '2-digit',
  day:   '2-digit'
};

const dateTimeFormat = new Intl.DateTimeFormat('default', config);

// Instead of calling dateTimeFormat.format, call dateTimeFormat.formatToParts
// You can use destructuring to get the needed values
const [
  { value: month },
  { value: day },
  { value: year }
] = dateTimeFormat.formatToParts(today);

// This function returns the following array:
0: { type: "month",   value: "Apr"  }
1: { type: "literal", value: " "    }
2: { type: "day",     value: "09"   }
3: { type: "literal", value: ", "   }
4: { type: "year",    value: "2020" }

// This will return the following formatted date: "2020/04/09"
console.log(`${year}/${month}/${day}`);
format.js

Writing a custom function, you can also specify a delimiter of your choice and the order of year-month-day:

Copied to clipboard! Playground
// Continuing from the previous example, we get the year, month and day
// from `dateTimeFormat.formatToParts`

// Define an object that holds the date information, with a `year`, `month`,
// and `day` node
const date = {
    year,
    month,
    day
};

// The output of this object will be:
{ year: "2020", month: "04", day: "09" }

const formatDate = (date, format) => {
    return format.replace('YYYY', date.year)
                 .replace('MM', date.month)
                 .replace('DD', date.day);
};

formatDate(date, 'YYYY-MM-DD'); // returns "2020-04-09"
formatDate(date, 'YYYY/MM/DD'); // returns "2020/04/09"
formatDate(date, 'YYYY:MM:DD'); // returns "2020:04:09"
formatDate(date, 'DD-MM-YYYY'); // returns "09-04-2020"
formatDate(date, 'MM-DD-YYYY'); // returns "04-09-2020"
format.js

Since format.replace returns a string, we can chain the function calls and keep calling replace until we replaced all values from the passed blueprint.

Now you can have all the dates you want. Do you have experience with the Date object in JavaScript? Let us know your favorite tricks in the comments!

How to Get the Date of Tomorrow in JavaScript
If you would like to see more Webtips, follow @flowforfrank

Resources

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.