Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get previous and next month in calendar-Swift

Tags:

I am trying to get previous and next month by click on button but this has changed my year, not month.

I am trying it from using this answer but it is 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 it for both previous and next month?

like image 249
iDeveloper Avatar asked Oct 01 '16 08:10

iDeveloper


People also ask

How do I get current month and year in Swift?

var lastMonthDate = Calendar. current. date(byAdding: . month, value: -1, to: date) calendar.

How do I get the last 7 days from a date in Swift?

Same strategy as @MartinR answer, as short as possible: let cal = NSCalendar. currentCalendar() let date = NSDate() var result = map(-6... 0) { delta -> Int in cal.

How do I get the start date in Swift?

day = -1 return Calendar. current. date(byAdding: comp, to: self. startOfMonth()!) } }


1 Answers

Try like this.

To get date of next month.

Swift 3 and Swift 4

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 date of 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 date of next and previous month I have used todays date, you can use date from which you want next and previous month date, just change NSDate() with your NSDate object.

Edit: For continues traveling of next and previous months you need to store one date object currentDate and when you try to get next and previous month use that date object and update it.

For next month.

let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentDate, options: []) 

For previous month.

let currentDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentDate, options: []) 

You can also use extension that will make thing easy.

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 get date from currentDate.

currentDate = currentDate.getNextMonth() currentDate = currentDate.getPreviousMonth() 
like image 133
Nirav D Avatar answered Nov 08 '22 16:11

Nirav D