Get Same Date Next Month: How to Do It
In this article
To get the same date next month, add one month to the current date, but be careful: if the next month has fewer days (e.g., going from January 31 to February), the date rolls over to the last day of that month. This adjustment is the core issue in every programming language and spreadsheet formula, and this article explains the logic, common solutions, and pitfalls.
Why “Same Date Next Month” Is Tricky
Months have different lengths: 28, 30, or 31 days. When you add a fixed number of days (like 30) to a date, you don’t always land on the same calendar day. For example, from March 15, adding 30 days gives April 14, not April 15. To get the “same date” (the same day number) in the next month, you must work with the month component of the date, not just add days.
The real complexity arises for day numbers that don’t exist in every month. The 31st of January has no equivalent in February. The 30th of February also doesn’t exist. So any robust method must define a rule for these edge cases. Most systems default to the last day of the target month, but some might roll to March 1 or March 2, depending on the implementation.
How It Works in Programming Languages
Different languages handle this differently, and it’s easy to get unexpected results if you don’t know the behavior.
- Python (using
dateutil): Therelativedeltafunction adds one month and automatically clamps to the last day. For example,datetime.date(2024, 1, 31) + relativedelta(months=1)returns2024-02-29(because 2024 is a leap year). - JavaScript (using
setMonth): If you dodate.setMonth(date.getMonth() + 1), JavaScript will overflow. For January 31, it becomes March 2 or March 3 (depending on the year), because February doesn’t have 31 days. This is a common source of bugs. - Excel: The
EDATEfunction returns the same day of the month, but if that day doesn’t exist, it returns the last day of that month. For example,EDATE(DATE(2024,1,31),1)returns February 29, 2024. - PHP:
strtotime('+1 month')behaves like JavaScript—it overflows. For January 31, it gives March 2 or 3.
Always check the documentation for your specific language or library. The difference between “clamp to last day” and “overflow to next month” is the key decision you need to make.
How to Do It in Excel and Google Sheets
In Excel and Google Sheets, the EDATE function is the standard tool. It takes two arguments: the start date and the number of months to add (can be negative). It returns a serial number that you format as a date.
Example: =EDATE(A1, 1) gives the same date one month after the date in cell A1. If A1 contains January 31, 2024, the result is February 29, 2024. If A1 contains March 31, 2024, the result is April 30, 2024.
If you need to avoid the last-day clamping and instead want to roll over to the next month (like JavaScript), you could use a formula that adds days based on the number of days in the current month. But that’s rarely what people want. The EDATE behavior is the most intuitive for most business use cases (e.g., monthly subscriptions, payment due dates).
Common Problems and Misconceptions
One common misconception is that adding 30 days gives you the same date next month. That only works if the starting date is the 30th or 31st of a month that has 30 or 31 days, and the next month also has that many days. For example, from March 15, adding 30 days gives April 14, not April 15. So never use +30 days unless you specifically want a 30-day interval, not a calendar month.
Another issue is leap years. February 29 only exists in leap years. If you ask for the “same date” one month after February 29, 2024, the next month is March, and March 29 exists, so you get March 29, 2024. But if you ask for the same date one month before February 29, 2024, you get January 29, 2024, because January has 29 days. That’s fine. The problem only arises when the target month doesn’t have the day number at all.
Also, be aware of time zones. If you’re working with dates that include times, adding a month might shift the time due to daylight saving time changes. In most cases, the time remains the same, but the day might be off by one hour in some regions. For pure date calculations, this is irrelevant.
How to Choose the Right Method
First, decide what you need the result for. If you’re calculating a monthly billing cycle, you probably want the last day of the month if the original day doesn’t exist. For example, a subscription that starts on January 31 should renew on February 28 (or 29 in a leap year), not on March 2.
If you’re scheduling a recurring event and you want the event to always fall on the last day of the month, you might want to check if the original date is the last day of its month, and then always use the last day of the next month. This is a common pattern for payroll or rent.
If you’re doing date arithmetic in code, you should use a well-tested library (like dateutil in Python or Moment.js in JavaScript) rather than writing your own logic. Libraries handle edge cases consistently.
Alternatives: Adding Days vs. Adding Months
Sometimes you don’t actually want the same calendar date; you want a fixed number of days. For example, a 30-day trial or a 90-day warranty. In those cases, adding days is correct. But if you want the same day number in the next month, you must add months.
Here’s a quick comparison:
- Add 1 month: Same day number, clamped to last day if necessary. Good for recurring dates (e.g., rent due on the 1st).
- Add 30 days: Exactly 30 days later. Good for fixed-length periods (e.g., 30-day notice).
- Add 1 month and then subtract 1 day: Gives the last day of the current month. This is a common trick to get month-end dates.
For example, to get the last day of the next month from January 15, 2024, you could use EDATE(DATE(2024,1,15), 2) - 1 in Excel, which gives February 29, 2024.
Frequently Asked Questions
What happens if I add one month to January 31 in JavaScript?
In JavaScript, new Date(2024, 0, 31).setMonth(1) will produce March 2, 2024, because February 31 doesn’t exist and the extra days spill over into March. This is a common bug. To get the last day of February, you need to manually check the day of the month and set it to the last day if it overflows.
How do I get the same date next month in SQL?
In SQL Server, you can use DATEADD(MONTH, 1, date). This function clamps to the last day of the month. For example, DATEADD(MONTH, 1, '2024-01-31') returns '2024-02-29'. In MySQL, you can use DATE_ADD(date, INTERVAL 1 MONTH), which also clamps. In PostgreSQL, date + interval '1 month' also clamps.
Does the same date next month always exist?
No. Only dates from the 1st to the 28th exist in every month. The 29th, 30th, and 31st may not exist in February (and the 31st doesn’t exist in April, June, September, or November). So for those dates, you must decide whether to clamp to the last day or roll over to the next month.
Why does adding 30 days not give the same date?
Because months have different lengths. Adding 30 days to March 15 gives April 14, not April 15. To keep the day number the same, you must add a month, not days. The only time adding 30 days works is when the starting date is the 30th or 31st and both months have that day.
Is there a universal formula for this in Excel?
Yes, EDATE(start_date, 1) is the universal formula. It handles all month lengths and leap years automatically. If you need the last day of the next month, use EDATE(start_date, 2) - 1. For the first day of the next month, use EDATE(start_date, 1) - DAY(start_date) + 1.