更新时间:2023-11-05 17:51:52
想必你已经有了一个Date
?
let weekday = Calendar.current.component(.weekday, from: myDate)
也许您想要工作日的名称?
Maybe you want the name of the weekday?
let f = DateFormatter()
f.weekdaySymbols[Calendar.current.component(.weekday, from: Date()) - 1]
最后添加 -1
是因为 Calendar.current.component(.weekday, from: Date()) 返回 1-7 之间的值,但 weekdaySymbols 需要数组索引.
The -1
is added at the end because Calendar.current.component(.weekday, from: Date()) returns values from 1-7 but weekdaySymbols expects array indices.
调查 NSCalendar
/DateComponents
api 或编辑您的问题以使其更具体,如果您需要 DateComponents api 的帮助.
Investigate the NSCalendar
/DateComponents
api or edit your question to be more specific if you need help with the DateComponents api.