How to print name of the day of the week?

后端 未结 2 1886
余生分开走
余生分开走 2020-12-20 10:14

Im trying to print out the name of the day of the week i.e Monday, Tuesday, Wednesday. I currently have this bit of code that does just that. I was wondering if there is a w

2条回答
  •  庸人自扰
    2020-12-20 11:12

    Use this function:

    func DayOfWeek(date: String) -> String?
    {
        let dateFormatter  = DateFormatter()
        dateFormatter.dateFormat = "yyyy-M-d"
        dateFormatter.locale = Locale(identifier: "en_US_POSIX")
        guard let _date = dateFormatter.date(from: date) else { return nil }
        let weekday = Calendar(identifier: .gregorian).component(.weekday, from: _date)
        return Calendar.current.weekdaySymbols[weekday-1]
    }
    

提交回复
热议问题