週末かどうかを判定したいのであれば、isDateInWeekend(_:)
を使えば良いのではないかと。
isDateInWeekend(_:) | Apple Developer Documentation
isDateInWeekend(_:)
Returns a Boolean value indicating whether the given date is within a weekend period.
Declaration
func isDateInWeekend(_ date: [Date](https://developer.apple.com/documentation/foundation/date)) -> [Bool](https://developer.apple.com/documentation/swift/bool)
Parameters
date
The specified date.
Return Value
true
if the given date is within a weekend, as defined by the calendar and calendar’s locale; otherwise, false
.
swift
1let calendar = Calendar(identifier: .gregorian)
2let formatter = ISO8601DateFormatter()
3formatter.timeZone = TimeZone(abbreviation: "JST")
4let date = Date()
5print(formatter.string(from: date))
6print(calendar.isDateInWeekend(date))
7
8guard let may05 = calendar.date(from: DateComponents(year: 2021, month: 5, day: 5)) else {return}
9print(formatter.string(from: may05)) // 2021-05-05T00:00:00+09:00
10print(calendar.isDateInWeekend(may05)) // false
11
12guard let may08 = calendar.date(from: DateComponents(year: 2021, month: 5, day: 8)) else {return}
13print(formatter.string(from: may08)) // 2021-05-08T00:00:00+09:00
14print(calendar.isDateInWeekend(may08)) //true
15
16guard let may09 = calendar.date(from: DateComponents(year: 2021, month: 5, day: 9)) else {return}
17print(formatter.string(from: may09)) // 2021-05-09T00:00:00+09:00
18print(calendar.isDateInWeekend(may09)) // true
19
20guard let may10 = calendar.date(from: DateComponents(year: 2021, month: 5, day: 10)) else {return}
21print(formatter.string(from: may10)) // 2021-05-10T00:00:00+09:00
22print(calendar.isDateInWeekend(may10)) // false
23
formatter
は出力用です。判定には不要です。もちろん祝日には非対応です。