How to add one month to an NSDate?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Adding one month to an NSDate is not the same as adding thirty days. Calendar months have different lengths, so the correct approach is to use Calendar and date components rather than raw second arithmetic.
Prefer Date and Calendar
In modern Swift, Date is the idiomatic type and bridges automatically to NSDate. To add one month, use Calendar.date(byAdding:value:to:):
If you already have an NSDate, bridge it:
This is the correct calendar-aware solution. It respects month boundaries instead of pretending every month has a fixed duration.
Why Not Add Seconds Manually
A month is not a fixed number of seconds. Adding 30 * 24 * 60 * 60 can give the wrong result for:
- February
- months with 31 days
- daylight saving time transitions
That is why Foundation provides calendar-based arithmetic. Dates represent points in time. Calendars define how humans interpret those points as months, days, and years.
Month-End Behavior Matters
Consider January 31 plus one month. What should the result be? Calendar arithmetic resolves that using the calendar’s rules:
The result will land on the last valid day in the target month according to Foundation’s calendar behavior. That is almost always what you want for business and UI logic.
Use a Specific Calendar and Time Zone When Precision Matters
Calendar.current reflects the user’s current locale and time zone. That is fine for many app features, but for repeatable behavior, use an explicit calendar and time zone:
This is a better choice for tests, server-aligned logic, and anything where a user’s local settings should not change the result.
Add Multiple Components Safely
If you need more than one date change, use DateComponents:
This keeps the operation declarative and calendar-aware.
Use Calendar Math for Recurring Rules
This matters most in recurring features such as billing cycles, subscriptions, or reminder dates. Those features usually care about “same day next month” rather than “thirty days later”. Calendar expresses that intent directly, which makes the code easier to review and much less fragile.
Common Pitfalls
- Treating a month as a fixed number of days or seconds.
- Doing date math with raw timestamps when the real problem is calendar arithmetic.
- Forgetting that
NSDateandDatebridge cleanly, then writing unnecessary conversion code. - Ignoring time zone and locale when consistent results matter across devices or tests.
- Being surprised by end-of-month behavior without deciding what the business rule should be.
Summary
- Add one month with
Calendar.date(byAdding: .month, value: 1, to: ...). - Prefer calendar arithmetic over manual second arithmetic.
- Bridge
NSDatetoDateand back when needed instead of using older APIs directly. - Be explicit about calendar and time zone when deterministic behavior matters.
- Test end-of-month cases because they are where date logic usually breaks.
Related reading
- How to add Options Menu to Fragment in Android
- How to add spacing between UITableViewCell
- How to add TextField to UIAlertController in Swift
- How to adjust an UIButton's imageSize?
- How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?
- How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?
- How to adjust layout when soft keyboard appears
- How to affect Delphi XEx code generation for Android/ARM targets?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.