Get Weekday from Date - Swift 3

后端 未结 6 968
时光说笑
时光说笑 2021-01-01 08:21

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(.week         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 09:25

    Presumably you have a Date already?

    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]
    

    The -1 is added at the end because Calendar.current.component(.weekday, from: Date()) returns values from 1-7 but weekdaySymbols expects array indices.

    Investigate the NSCalendar/DateComponents api or edit your question to be more specific if you need help with the DateComponents api.

提交回复
热议问题