Today is Tuesday, March 13, 2018. The previous Sunday was March 11, 2018. My code for calculating the previous Sunday:
// NOTE that this particular code block may return the correct result for you IF your timezone // is something other than "America/Chicago". Eg "America/Belize". Use the longer code // block that begins with "var calendar = ..." if you are trying to replicate the issue Calendar.current.nextDate(after: Date(), matching: DateComponents(weekday: 1), matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: .backward)!
This returns the date of March 4, not the expected value of March 11. This is due to the DST change . I tried every combination of matchingPolicy and repeatedTimePolicy , but always returns on March 4th. Any thoughts on how I can return the calendar on March 11th?
var calendar = Calendar(identifier: .gregorian) calendar.locale = Locale(identifier: "en_US") calendar.timeZone = TimeZone(identifier: "America/Chicago")! // Tuesday March 13th @ 3:10 PM (America/Chicago) let today = Date(timeIntervalSince1970: 1520971846) let d1 = calendar.nextDate(after: today, matching: DateComponents(weekday: 1), matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: .backward)! let d2 = calendar.nextDate(after: today, matching: DateComponents(weekday: 1), matchingPolicy: .nextTimePreservingSmallerComponents, repeatedTimePolicy: .first, direction: .backward)! let d3 = calendar.nextDate(after: today, matching: DateComponents(weekday: 1), matchingPolicy: .previousTimePreservingSmallerComponents, repeatedTimePolicy: .first, direction: .backward)! let d4 = calendar.nextDate(after: today, matching: DateComponents(weekday: 1), matchingPolicy: .strict, repeatedTimePolicy: .first, direction: .backward)! let d5 = calendar.nextDate(after: today, matching: DateComponents(weekday: 1), matchingPolicy: .nextTime, repeatedTimePolicy: .last, direction: .backward)! let d6 = calendar.nextDate(after: today, matching: DateComponents(weekday: 1), matchingPolicy: .nextTimePreservingSmallerComponents, repeatedTimePolicy: .last, direction: .backward)! let d7 = calendar.nextDate(after: today, matching: DateComponents(weekday: 1), matchingPolicy: .previousTimePreservingSmallerComponents, repeatedTimePolicy: .last, direction: .backward)! let d8 = calendar.nextDate(after: today, matching: DateComponents(weekday: 1), matchingPolicy: .strict, repeatedTimePolicy: .last, direction: .backward)!
An error report has been submitted for the proposal.