Swift 日历,工作日从星期一而不是星期日开始
Swift Calendar, Weekday starting on Monday instead Sunday
伙计们,我正在按照教程使用 SwiftUI 在我的应用程序中显示日历
一切正常,但我需要:
从星期一而不是星期日显示星期的名称
并根据一周的第一天更改那几天
现在我已经做到了,我该如何实现我的目标?
private extension Calendar {
func generateDates(for interval: DateInterval, matching components: DateComponents) -> [Date] {
var dates = [interval.start]
enumerateDates(startingAfter: interval.start, matching: components, matchingPolicy: .nextTime) { result, _, stop in
guard let date = result else { return }
guard date < interval.end else {
stop = true
return
}
dates.append(date)
}
return dates
}
func generateDays(for interval: DateInterval) -> [Date] {
generateDates(for: interval, matching: dateComponents([.hour, .minute, .second], from: interval.start))
}
}
func makeDays() -> [Date] {
guard let firstWeek = calendar.dateInterval(of: .weekOfMonth, for: selectedDate),
let lastWeek = calendar.dateInterval(of: .weekOfMonth, for: firstWeek.end - 1) else { return [] }
let interval = DateInterval(start: firstWeek.start, end: lastWeek.end)
return calendar.generateDays(for: interval)
}
创建自定义公历并将第一个工作日设置为星期一
var customCalendar = Calendar(identifier: .gregorian)
customCalendar.firstWeekday = 2
伙计们,我正在按照教程使用 SwiftUI 在我的应用程序中显示日历
一切正常,但我需要: 从星期一而不是星期日显示星期的名称 并根据一周的第一天更改那几天
现在我已经做到了,我该如何实现我的目标?
private extension Calendar {
func generateDates(for interval: DateInterval, matching components: DateComponents) -> [Date] {
var dates = [interval.start]
enumerateDates(startingAfter: interval.start, matching: components, matchingPolicy: .nextTime) { result, _, stop in
guard let date = result else { return }
guard date < interval.end else {
stop = true
return
}
dates.append(date)
}
return dates
}
func generateDays(for interval: DateInterval) -> [Date] {
generateDates(for: interval, matching: dateComponents([.hour, .minute, .second], from: interval.start))
}
}
func makeDays() -> [Date] {
guard let firstWeek = calendar.dateInterval(of: .weekOfMonth, for: selectedDate),
let lastWeek = calendar.dateInterval(of: .weekOfMonth, for: firstWeek.end - 1) else { return [] }
let interval = DateInterval(start: firstWeek.start, end: lastWeek.end)
return calendar.generateDays(for: interval)
}
创建自定义公历并将第一个工作日设置为星期一
var customCalendar = Calendar(identifier: .gregorian)
customCalendar.firstWeekday = 2