Date Arithmetic in JavaScript
Add and subtract days, months, years and hours in JavaScript with set methods and timestamps, and find date differences. Explanation and output.
The sample output was captured with the clock set to 15 January 2024, 10:30:45 UTC. Your run will show your own date, time and time zone.
1// Method 1: Add/subtract days2function addDays(date, days) {3 const result = new Date(date);4 result.setDate(result.getDate() + days);5 return result;6}78function subtractDays(date, days) {9 return addDays(date, -days);10}1112const today = new Date();13console.log('Today:', today);14console.log('Tomorrow:', addDays(today, 1));15console.log('Yesterday:', subtractDays(today, 1));1617// Method 2: Add/subtract months18function addMonths(date, months) {19 const result = new Date(date);20 result.setMonth(result.getMonth() + months);21 return result;22}2324console.log('Next month:', addMonths(today, 1));25console.log('Last month:', addMonths(today, -1));2627// Method 3: Add/subtract years28function addYears(date, years) {29 const result = new Date(date);30 result.setFullYear(result.getFullYear() + years);31 return result;32}3334console.log('Next year:', addYears(today, 1));3536// Method 4: Add/subtract hours37function addHours(date, hours) {38 const result = new Date(date);39 result.setHours(result.getHours() + hours);40 return result;41}4243console.log('In 2 hours:', addHours(today, 2));4445// Method 5: Add/subtract minutes46function addMinutes(date, minutes) {47 const result = new Date(date);48 result.setMinutes(result.getMinutes() + minutes);49 return result;50}5152console.log('In 30 minutes:', addMinutes(today, 30));5354// Method 6: Using timestamps55function addTime(date, milliseconds) {56 return new Date(date.getTime() + milliseconds);57}5859console.log('Add 1 day (ms):', addTime(today, 24 * 60 * 60 * 1000));60console.log('Add 1 hour (ms):', addTime(today, 60 * 60 * 1000));6162// Method 7: Date difference63function dateDifference(date1, date2) {64 const diff = Math.abs(date1 - date2);65 const days = Math.floor(diff / (1000 * 60 * 60 * 24));66 const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));67 const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));68 return { days, hours, minutes };69}7071const date1 = new Date('2024-01-01');72const date2 = new Date('2024-01-15');73console.log('Difference:', dateDifference(date1, date2));
Sample Output
Today: 2024-01-15T10:30:45.123Z
Tomorrow: 2024-01-16T10:30:45.123Z
Yesterday: 2024-01-14T10:30:45.123Z
Next month: 2024-02-15T10:30:45.123Z
Last month: 2023-12-15T10:30:45.123Z
Next year: 2025-01-15T10:30:45.123Z
In 2 hours: 2024-01-15T12:30:45.123Z
In 30 minutes: 2024-01-15T11:00:45.123Z
Add 1 day (ms): 2024-01-16T10:30:45.123Z
Add 1 hour (ms): 2024-01-15T11:30:45.123Z
Difference: { days: 14, hours: 0, minutes: 0 }In this one
- How to add or subtract days, months and years
- Why you copy the date before changing it
- When to use set methods or timestamps
- How to compute the gap between two dates
How it works
- 1
today is printed, then tomorrow and yesterday.
- 2
Next and last month are computed with setMonth.
- 3
Next year uses setFullYear.
- 4
Hours and minutes are added.
- 5
One day and one hour are added as milliseconds.
- 6
The difference between 1 and 15 January is 14 days.
Edge cases
End of month
addMonths(new Date(2024, 0, 31), 1) gives 2 March 2024.
February 2024 has 29 days, so the extra days roll over. Clamp to the last day if needed.
Daylight saving time
Adding 24 hours of milliseconds can land at 23:00 or 01:00.
Use setDate for calendar days in regions with DST.
Mutating the original
today.setDate(today.getDate() + 1) changes today itself.
Copy first with new Date(today).
What's going on
Date arithmetic means moving a date forward or back, or measuring the gap between two dates. A library app adds 14 days to find a due date. A fee reminder subtracts days to warn early.
Every helper here starts with new Date(date). That makes a copy. Date set methods change the object they are called on, so copying first means the original date is not mutated. This is a best practice. Forget it and "today" quietly becomes "tomorrow" everywhere.
Days use setDate(getDate() + n). Months use setMonth(getMonth() + n). Years use setFullYear(getFullYear() + n). Hours and minutes use setHours and setMinutes the same way. To subtract, pass a negative value. subtractDays simply calls addDays with -days.
JavaScript handles overflow for you. Adding 1 day to 31 January gives 1 February. Adding hours past midnight moves to the next day. That is why these set methods are safer than building dates by hand.
Month and year boundaries need care. Adding 1 month to 31 January asks for 31 February, which does not exist, so JavaScript rolls over to 2 or 3 March. Check the result if your dates are near the end of a month.
Method 6 uses timestamps. It adds milliseconds directly to getTime(). This is precise, but a day is not always 24 hours when daylight saving time changes. For calendar days, setDate is safer. For exact durations, timestamps are right.
Method 7 finds the difference. Subtracting two dates gives milliseconds. Math.abs makes it positive. Dividing and using % (remainder) turns it into days, hours and minutes. 1 January to 15 January is 14 days. Remember time zones when your dates come from different places.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why copy the date with new Date(date) first?
Pick an answer to check it.
Question 2
What does new Date(2024, 0, 32) give?
Pick an answer to check it.
Questions people ask
Why does subtracting two dates give a number?
Dates convert to their timestamps, so you get milliseconds.
How many milliseconds are in a day?
1000 × 60 × 60 × 24 = 86,400,000.
Should I use a library?
For heavy date work, libraries like date-fns help. The Temporal API is the future built-in.