I Datum Erweiterungen schrieb Datum für bestimmten Wochentag zu bekommen und hier ist, wie einfach es mit Swift zu verwenden ist 4 ,
Date.today().next(.monday) // Feb 12, 2018 at 12:00 AM
Date.today().next(.sunday) // Feb 11, 2018 at 12:00 AM
Date.today().previous(.sunday) // Feb 4, 2018 at 12:00 AM
Date.today().previous(.monday) // Feb 5, 2018 at 12:00 AM
Date.today().previous(.thursday) // Feb 1, 2018 at 12:00 AM
Date.today().next(.thursday) // Feb 15, 2018 at 12:00 AM
Date.today().previous(.thursday,
considerToday: true) // Feb 8, 2018 at 11:04 PM
Date.today().next(.monday)
.next(.sunday)
.next(.thursday) // Feb 22, 2018 at 12:00 AM
Und hier ist seit Erweiterung für das,
extension Date {
static func today() -> Date {
return Date()
}
func next(_ weekday: Weekday, considerToday: Bool = false) -> Date {
return get(.Next,
weekday,
considerToday: considerToday)
}
func previous(_ weekday: Weekday, considerToday: Bool = false) -> Date {
return get(.Previous,
weekday,
considerToday: considerToday)
}
func get(_ direction: SearchDirection,
_ weekDay: Weekday,
considerToday consider: Bool = false) -> Date {
let dayName = weekDay.rawValue
let weekdaysName = getWeekDaysInEnglish().map { $0.lowercased() }
assert(weekdaysName.contains(dayName), "weekday symbol should be in form \(weekdaysName)")
let searchWeekdayIndex = weekdaysName.index(of: dayName)! + 1
let calendar = Calendar(identifier: .gregorian)
if consider && calendar.component(.weekday, from: self) == searchWeekdayIndex {
return self
}
var nextDateComponent = DateComponents()
nextDateComponent.weekday = searchWeekdayIndex
let date = calendar.nextDate(after: self,
matching: nextDateComponent,
matchingPolicy: .nextTime,
direction: direction.calendarSearchDirection)
return date!
}
}
// MARK: Helper methods
extension Date {
func getWeekDaysInEnglish() -> [String] {
var calendar = Calendar(identifier: .gregorian)
calendar.locale = Locale(identifier: "en_US_POSIX")
return calendar.weekdaySymbols
}
enum Weekday: String {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
}
enum SearchDirection {
case Next
case Previous
var calendarSearchDirection: Calendar.SearchDirection {
switch self {
case .Next:
return .forward
case .Previous:
return .backward
}
}
}
}
Sie Kalender ISO8601, wo die ersten verwenden wochentag ist montag und nicht ändern firstWeekday zu 1 (Sonntag) lassen kalender = NSCalendar (calendarIdentifier: NSCalendarIdentifierISO8601)! –
Ihre Frage ist mir unklar. Wenn Sie 'calendar.firstWeekday = 1 'festlegen, wird Sonntag als erster Tag der Woche definiert, sodass Ihr Ergebnis erwartet wird. –