Calculate the previous Sunday when the DST changed the previous Sunday

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.

+3
ios swift
source share
2 answers

This is probably Apple's mistake, as stated in the comment above.

There is a very ugly workaround, first discovering the โ€œnextโ€ Sunday, and then using this date to find the previous Sunday. I am sure that there are some extreme cases when this does not work, but maybe this can help you.

 // Not recommended for production code var nextSunday = Calendar.current.nextDate(after: Date(), matching: DateComponents(weekday: 1), matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: .forward)! var lastSunday = Calendar.current.nextDate(after: nextSunday, matching: DateComponents(weekday: 1), matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: .backward)! 

enter image description here

+1
source share

Another option is to subtract seven days from โ€œtoday,โ€ and then simply search for .forward . Personally, I will not look for .backward because of this problem with DST and because of this other message .

 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) var date = Date(timeIntervalSince1970: 1520971846) date = calendar.date(byAdding: .day, value: -7, to: date)! //or = calendar.date(byAdding: .weekOfYear, value: -1, to: date)! let lastSunday = calendar.nextDate(after: date, matching: DateComponents(weekday: 1), matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: .forward)! 
0
source share

All Articles