Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

ios - How to print name of the day of the week?

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 way to get rid of my switch statement and make this better. Thanks!

 func getDayOfWeek(_ today: String) -> String? {
    let formatter  = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    guard let todayDate = formatter.date(from: today) else { return nil }
    let myCalendar = Calendar(identifier: .gregorian)
    let weekDay = myCalendar.component(.weekday, from: todayDate)

    switch weekDay {
    case 1:
        return "Sunday"
    case 2:
        return "Monday"
    case 3:
        return "Tuesday"
    case 4:
        return "Wednesday"
    case 5:
        return "Thursday"
    case 6:
        return "Friday"
    case 7:
        return "Saturday"
    default:
        return ""
    }
}



getDayOfWeek("2018-3-5")

This prints out "Monday"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are using the wrong date format. The correct format is "yyyy-M-d". Besides that you can use Calendar property weekdaySymbols which returns the weekday localized.

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

Another option is to use DateFormatter and set your dateFormat to "cccc" as you can see in this answer:

extension Formatter {
    static let weekdayName: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "cccc"
        return formatter
    }()
    static let customDate: DateFormatter = {
        let formatter  = DateFormatter()
        formatter.dateFormat = "yyyy-M-d"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()
}
extension Date {
    var weekdayName: String { Formatter.weekdayName.string(from: self) }
}

Using the extension above your function would look like this:

func getDayOfWeek(_ date: String) -> String? { Formatter.customDate.date(from: date)?.weekdayName }

Playground testing:

getDayOfWeek("2018-3-5")  // Monday
Date().weekdayName        // Thursday

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...