My goal is to get the start date and end date of the week. So i found:
https://stackoverflow.com/a/46402722/4757272 which was a pretty neat solution. I however have some issues. Trying it out, the variable called sunday
is actually an saturday when i look in the calendar, thats pretty disturbing, but here comes the really disturbing part:
extension Date {
var startOfWeek: Date? {
let gregorian = Calendar(identifier: .gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
return gregorian.date(byAdding: .day, value: 1, to: sunday)
}
var endOfWeek: Date? {
let gregorian = Calendar(identifier: .gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
return gregorian.date(byAdding: .day, value: 7, to: sunday)
}
}
dump(Date().startOfWeek)
dump(Date().endOfWeek)
As you can see i print the result and as smooth as playground is it shows me the result in the right tab as well. However they are different dates.. so which one should i rely on?