检查 NSDate 是否在当前周不适用于特定日期?

Check if NSDate is in current week not working for specific date?

我尝试了几种方法来检查 NSDate 是否在本周:

Method 1

func isInThisWeek(date: Date) -> Bool
{
    return Calendar.current.isDate(date, equalTo: Date(), toGranularity: .weekOfYear)
}

Method 2

func dateFallsInCurrentWeek(date: Date) -> Bool
{
    let currentWeek = Calendar.current.component(Calendar.Component.weekOfYear, from: Date())
    let datesWeek = Calendar.current.component(Calendar.Component.weekOfYear, from: date)
    return (currentWeek == datesWeek)
} 

现在的情况是我得到 FALSE 虽然这个日期是在本周。

我测试了:Monday, August 10, 2020 6:00:00 PM (My time zone: +5:30 GMT)。所以根据日历,这个日期属于 10 Aug - 16 Aug 周。

可能出了什么问题?在我正在测试的 iPad 中,一周的开始日期是 Monday 如下:

所有日历都会将星期日视为第一个工作日。如果您想将星期一视为一周的开始,您需要使用 iso8601 日历。

let date = Date(timeIntervalSince1970: 1597516200) // .description  // "2020-08-15 18:30:00 +0000"
let tz = TimeZone(secondsFromGMT: 5*3600 + 1800)!  // GMT+0530 (fixed)
let components = Calendar.current.dateComponents(in: tz, from: date)
components.day      // 16
components.weekday  // Sunday
// Current Calendar starts on sunday so it goes from 9...15 and 16...22
// To get the current week starting on monday you need iso8601 calendar
let equalToDate = DateComponents(calendar: .current, timeZone: tz, year: 2020, month: 8, day: 10, hour: 18).date!
equalToDate.description  // "2020-08-10 12:30:00 +0000"
Calendar(identifier: .iso8601).isDate(date, equalTo: equalToDate, toGranularity: .weekOfYear)  // true