从日期获取工作日-Swift 3 [英] Get Weekday from Date - 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屋!