Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Getting tomorrow and day after tomorrow date in JavaScript
Using the Date class in JavaScript, we can easily calculate tomorrow and the day after tomorrow from the current date. The new Date() constructor returns a JavaScript Date object for the current day.
Getting Today's Date
First, let's get today's date using the Date constructor:
const today = new Date();
console.log("Today:", today.toDateString());
Today: Thu Aug 13 2020
Method 1: Using setDate() with Date Copying
We can create new Date objects by copying today's date and then adding days using the setDate() method:
// Getting today's date
const today = new Date();
// Creating tomorrow by copying today and adding 1 day
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
// Creating day after tomorrow by copying today and adding 2 days
const dayAfterTomorrow = new Date(today);
dayAfterTomorrow.setDate(dayAfterTomorrow.getDate() + 2);
console.log("Today:", today.toDateString());
console.log("Tomorrow:", tomorrow.toDateString());
console.log("Day after tomorrow:", dayAfterTomorrow.toDateString());
Today: Thu Aug 13 2020 Tomorrow: Fri Aug 14 2020 Day after tomorrow: Sat Aug 15 2020
Method 2: Using Milliseconds
Another approach is to add milliseconds to the current date. One day equals 24 hours × 60 minutes × 60 seconds × 1000 milliseconds:
const today = new Date();
const oneDayInMs = 24 * 60 * 60 * 1000; // milliseconds in a day
const tomorrow = new Date(today.getTime() + oneDayInMs);
const dayAfterTomorrow = new Date(today.getTime() + (2 * oneDayInMs));
console.log("Today:", today.toDateString());
console.log("Tomorrow:", tomorrow.toDateString());
console.log("Day after tomorrow:", dayAfterTomorrow.toDateString());
Today: Thu Aug 13 2020 Tomorrow: Fri Aug 14 2020 Day after tomorrow: Sat Aug 15 2020
How setDate() Handles Month Boundaries
The setDate() method automatically handles month and year boundaries. If adding days exceeds the current month, it rolls over to the next month:
// Example with month boundary
const endOfMonth = new Date(2023, 7, 31); // August 31, 2023
const nextDay = new Date(endOfMonth);
nextDay.setDate(nextDay.getDate() + 1);
console.log("End of month:", endOfMonth.toDateString());
console.log("Next day:", nextDay.toDateString()); // Automatically becomes September 1
End of month: Thu Aug 31 2023 Next day: Fri Sep 01 2023
Comparison
| Method | Readability | Handles DST? | Month Boundaries |
|---|---|---|---|
setDate() |
High | Yes | Automatic |
| Milliseconds | Medium | Potential issues | Manual calculation |
Conclusion
The setDate() method is the recommended approach for calculating future dates as it automatically handles month boundaries and leap years. Both methods work reliably for simple date arithmetic in JavaScript.
