Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026
The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.
Adding days to a date
You have a Date object in JavaScript, and you want to add some days to it. How do you do that?
Here is a date that represents today:
const my_date = new Date()
Suppose we want to get the date that’s “30 days from now”.
We use the setDate() and getDate() methods, in this way:
my_date.setDate(my_date.getDate() + 30)
Getting tomorrow’s date
This is what we’re going to do to get tomorrow’s date:
- we first get today’s date, using
new Date() - we set a new date by adding
1to it - done!
Using setDate() passing the result of <today>.getDate() + 1, you’ll set the day as “tomorrow”.
If the day is
31(in months with 31 days) and usingsetDate()you add1to the current one, the date will change month and the day will be the first of the new month. Or year, if it’s 31 December.
Here’s an example:
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
tomorrow is now a Date object representing tomorrow’s date. The time did not change - it’s still the time you ran the command, increased by 24 hours.
If you also want to reset the time to “tomorrow at 00:00:00”, you can do so by calling tomorrow.setHours(0,0,0,0).
Getting yesterday’s date
First you get the date at the current time (today), then you subtract a day from it:
const today = new Date()
const yesterday = new Date(today)
yesterday.setDate(yesterday.getDate() - 1)
today.toDateString()
yesterday.toDateString()
We use the setDate() method on yesterday, passing as parameter the current day minus one.
Even if it’s day 1 of the month, JavaScript is logical enough and it will point to the last day of the previous month.
Counting days between dates
How do you calculate the number of days between 2 dates?
Starting from the starting date, we add one day until the date represents a date after the end date:
const numberOfNightsBetweenDates = (startDate, endDate) => {
const start = new Date(startDate) //clone
const end = new Date(endDate) //clone
let dayCount = 0
while (end > start) {
dayCount++
start.setDate(start.getDate() + 1)
}
return dayCount
}
We first clone the dates we are given, because dates are objects, and we get a reference to that object. This means that using setDate() in the function would also affect the variable outside of this function - not something we look forward to!
If instead you want to get the number of days between 2 dates (say, from one day to the next is 2 days), just change while (end > start) to while (end >= start). That would work. Or increase the dayCount starting point to 1.
Getting all dates between two dates
Given two JavaScript Date objects, how can I get a list of the days (expressed as Date objects, too) between those 2 dates?
Here’s a function to calculate that. It gets 2 date objects as parameters, and returns an array of Date objects:
const getDatesBetweenDates = (startDate, endDate) => {
let dates = []
//to avoid modifying the original date
const theDate = new Date(startDate)
while (theDate < endDate) {
dates = [...dates, new Date(theDate)]
theDate.setDate(theDate.getDate() + 1)
}
return dates
}
Example usage:
const today = new Date()
const threedaysFromNow = new Date(today)
threedaysFromNow.setDate(threedaysFromNow.getDate() + 3)
getDatesBetweenDates(today, threedaysFromNow)
If you also want to include the start and end dates, you can use this version that adds it at the end:
const getDatesBetweenDates = (startDate, endDate) => {
let dates = []
//to avoid modifying the original date
const theDate = new Date(startDate)
while (theDate < endDate) {
dates = [...dates, new Date(theDate)]
theDate.setDate(theDate.getDate() + 1)
}
dates = [...dates, endDate]
return dates
} Lessons in this unit:
| 0: | Introduction |
| 1: | Date Basics |
| 2: | Getting Date Parts |
| 3: | Date Formatting |
| 4: | ▶︎ Date Math |
| 5: | Date Comparisons |
| 6: | Date Recipes |