I need to set UILocalNotification, I just need to take hour and minute from DatePicker and need to set specific date ( like : Monday ) and repeat it on every Monday.>
Swift 3:
func calculateSpecificDay(_ desiredWeekday: NSInteger, fromDate: Date = Date()) -> Date {
let weekDateRange = NSCalendar.current.maximumRange(of: .weekday)!
let daysInWeek = weekDateRange.lowerBound.distance(to: weekDateRange.upperBound) - weekDateRange.lowerBound + 1
let currentWeekday = NSCalendar.current.dateComponents([.weekday], from: fromDate).weekday
let differenceDays = (desiredWeekday - currentWeekday! + daysInWeek) % daysInWeek
var dateComponents = DateComponents()
dateComponents.day = differenceDays
let calculatedFridayDate = NSCalendar.current.date(byAdding: dateComponents, to: fromDate)
return calculatedFridayDate!
}
If selected day is sunday then pass 1 If selected day is monday then pass 2 . . . If selected day is saturday then pass 7