且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从日期获取工作日-Swift 3

更新时间:2023-11-05 17:30:16

大概您已经有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.