从日期获取工作日 - Swift 3 [英] Get Weekday from Date - Swift 3
问题描述
我环顾四周,但没有在 swift 3 中找到一个快速答案.我今天的工作日是这样的:
I have looked around a bit, but didnt find a quick answer for this in swift 3. I get todays weekday like this:
let weekday = Calendar.current.component(.weekday, from: Date())
但相反,我想获取给定日期的工作日.这是怎么做的?谢谢
But instead, i would like to get the weekday of a given date. How is this done? Thanx
推荐答案
想必你已经有了一个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.
这篇关于从日期获取工作日 - Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!