How to get the previous and next month in the Swift calendar

I try to get the previous and next month by clicking a button, but that changed my year, not the month.

I am trying to use this link , but located in Objective-C.

func firstDateOfMonth(){ let components = calendar.components([.Year, .Month], fromDate: date) let startOfMonth = calendar.dateFromComponents(components)! print(dateFormatatter.stringFromDate(startOfMonth)) // 2015-11-01 } func lastDateOfMonth(){ let components = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: date) let month = components.month let year = components.year let startOfMonth1 = ("01-\(month)-\(year)") var startOfMonth : NSDate? var lengthOfMonth : NSTimeInterval = 0 calendar.rangeOfUnit(.Month, startDate: &startOfMonth, interval: &lengthOfMonth, forDate: date) let endOfMonth = startOfMonth!.dateByAddingTimeInterval(lengthOfMonth - 1) print(dateFormatatter.stringFromDate(endOfMonth)) } func monthCalculation(){ firstDateOfMonth() lastDateOfMonth() } @IBAction func previousMonthBtnAction(sender: AnyObject) { let componen = calendar.components([.Day , .Month , .Year], fromDate: date) componen.month = -1 self.date = calendar.dateFromComponents(componen)! print(self.date) } @IBAction func nextMonthBtnAction(sender: AnyObject) { } 

So, how to do this both in the previous and next month?

+12
source share
3 answers

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() 
+30
source
 var currentDate = NSDate() func nextMonth() -> NSDate { let nextDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentDate, options: [])! currentDate = nextDate return nextDate } print(nextMonth()) // 2016-11-01 20:11:15 +0000 print(nextMonth()) // 2016-12-01 21:11:15 +0000 print(nextMonth()) // 2017-01-01 21:11:15 +0000 
+2
source

I would recommend using SwiftDate :

 let date = Date() let nextMonthDate = date.nextMonth(at: .auto) let prevMonthDate = date.prevMonth(at: .auto) 

You can use .start and .end instead of .auto . See information about nextMonth , for example:

when .auto date is calculated by adding one month to the current date.

If you pass .start the result date is the first day of the next month (at 00:00:00).

If you pass .end , the date of the result will be the last day of the next month (at 23:59:59).

The library also contains prevWeek / nextWeek and many other useful methods.

0
source

All Articles