Find NSDate for the next nearest certain day of the week for any date.

Suppose today is Wednesday. I can break NSDate down to NSDateComponents , but I need to find NSDate with next coming Monday. If today is Monday, then the next upcoming Monday is today. What is the right way to achieve this?

+7
cocoa-touch cocoa nsdate date-arithmetic
source share
3 answers

You can use the nextDateAfterDate: method on an nextDateAfterDate: object to achieve this,

 let now = Date() // today var matchingComponents = DateComponents() matchingComponents.weekday = 2 // Monday let comingMonday = Calendar.current.nextDate(after: now, matching: matchingComponents, matchingPolicy:.nextTime) 

Here is an easy way to find next Monday. If today is Monday, the next function returns today, or the next next Monday. Note that it uses en_POSIX_US , so that days can be matched. When the locale is ru_POSIX_US , the characters of the day of the week become,

 ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] 

And this is how you can use these days,

 func findNext(_ day: String, afterDate date: Date) -> Date? { var calendar = Calendar.current calendar.locale = Locale(identifier: "en_US_POSIX") let weekDaySymbols = calendar.weekdaySymbols let indexOfDay = weekDaySymbols.index(of: day) assert(indexOfDay != nil, "day passed should be one of \(weekDaySymbols), invalid day: \(day)") let weekDay = indexOfDay! + 1 let components = calendar.component(.weekday, from: date) if components == weekDay { return date } var matchingComponents = DateComponents() matchingComponents.weekday = weekDay // Monday let nextDay = calendar.nextDate(after: date, matching: matchingComponents, matchingPolicy:.nextTime) return nextDay! } let nextMonday = findNext("Monday", afterDate: Date()) let mondayAfterThat = findNext("Monday", afterDate: nextMonday!) let thursday = findNext("Thursday", afterDate: mondayAfterThat!) 
+20
source share

for target iOS> = 8 use GeneratorOfOne solution

else you can use this

 let now = NSDate() var lastMonday:NSDate? var nextMonday:NSDate? var start:NSDate? var interval:NSTimeInterval = 0 // holds the length of a week. can differ for Daylight Saving Time let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! // have a own calendar object for this calculation to guarantee that the next line does not influence other calculations cal.firstWeekday = 2 // make sure first day of week is Monday. cal.rangeOfUnit(NSCalendarUnit.CalendarUnitWeekOfMonth, startDate: &lastMonday, interval:&interval, forDate: now) // monday of current week nextMonday = lastMonday?.dateByAddingTimeInterval(interval) // add a weeks length to the last weeks monday println(nextMonday) 
+2
source share

You can get the working day using NSDateComponents and calculate the interval days, and then use dateByAddingTimeInterval from NSDate as follows:

 let now = NSDate() let calendar: NSDateComponents = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitWeekday, fromDate: now) let weekday = calendar.weekday // 1 = Sunday, 2 = Monday // You get input the weekday here and calculate the interval // exmaple for moving to the next day let expectedDate = now.dateByAddingTimeInterval(1 * 24 * 60 * 60) // exmaple for moving to yesterday let yesterday = now.dateByAddingTimeInterval(-1 * 24 * 60 * 60) 
-4
source share

All Articles