How to Get Tomorrow's Date in 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:
const tomorrow = new Date(); // The Date object returns today's timestamp
tomorrow.setDate(tomorrow.getDate() + 1);
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:
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
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:
const getTomorrow = () => new Date.setDate(new Date.getDate() + 1);
How to Format Dates
If you would also like to format your dates, you can use the DateTimeFormat
object.
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));
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:
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}`);
Writing a custom function, you can also specify a delimiter of your choice and the order of year-month-day:
// 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"
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!
Resources
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: