I need to get all the dates of a week for the current month only when I pressed a specific date. For example, if I pressed Sept 28, the result will be Sept 23-28 dates but if I pressed Sept 30, the result will be Sept 30 ONLY because the dates after that are for October.
This is my code to get the dates of a week:
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: 0, 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: 6, to: sunday)
}
}
But for this code the result is Sept 30 - Oct 6 which is not what I want. Pls help