Try it.
To get the date of the next month.
Swift 3
let nextMonth = Calendar.current.date(byAdding: .month, value: 1, to: Date())
Swift 2.3 or lower
let nextMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: NSDate(), options: [])
To get the date of the previous month.
Swift 3
let previousMonth = Calendar.current.date(byAdding: .month, value: -1, to: Date())
Swift 2.3 or lower
let previousMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: NSDate(), options: [])
Note. . To get the date of the next and previous month, I used today's date, you can use the date from which you want the next and previous date of the month, just change NSDate() to an NSDate object.
Edit: To continue the journey for the next and previous months, you need to save one date currentDate object, and when you try to get the next and previous month, use this date object and update it.
Over the next month.
let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentDate, options: [])
For the previous month.
let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentDate, options: [])
You can also use the extension, which will simplify the work.
Swift 3
extension Date { func getNextMonth() -> Date? { return Calendar.current.date(byAdding: .month, value: 1, to: self) } func getPreviousMonth() -> Date? { return Calendar.current.date(byAdding: .month, value: -1, to: self) } }
Swift 2.3 or lower
extension NSDate { func getNextMonth() -> NSDate?{ return NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: self, options: []) } func getPreviousMonth() -> NSDate?{ return NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: self, options: []) } }
Now just enter the date from currentDate.
currentDate = currentDate.getNextMonth() currentDate = currentDate.getPreviousMonth()