better way to get the name of the day on ios swift

27,022

Solution 1

Use DateFormatter

Swift 4

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let dayInWeek = dateFormatter.string(from: date)

Swift3

let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat  = "EEEE" // "EE" to get short style
let dayInWeek = dateFormatter.stringFromDate(date) // "Sunday"    

Screenshot

enter image description here

Solution 2

If you want to get the array of day names you could use: weekdaySymbols in Calendar()

example:

let calendar = Calendar(identifier: .gregorian)
let days = calendar.weekdaySymbols

Solution 3

You can check also this DateFormatter

let dayNameFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.locale = .current
    dateFormatter.calendar = .current
    dateFormatter.dateFormat = "cccc"
    return dateFormatter
}()
print(dayNameFormatter.string(from: Date())) Prints today's day name 🤪

c - stands for day of the week, good answer, official source

Share:
27,022

Related videos on Youtube

sarah
Author by

sarah

Updated on July 09, 2022

Comments

  • sarah
    sarah almost 2 years

    i take the name of the day like this

    func loadDayName(forDate date: NSDate) -> String{
        let myComponents = calendar.components(.Weekday, fromDate: date)
        let weekDay = myComponents.weekday
        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 "Nada"
        }
    }
    

    it is working find but it i was wondering if swift comes with some libraries to do that automatically better than me do that in code like manually

    • Darren
      Darren over 8 years
      You should use NSDateFormatter with a custom date format. developer.apple.com/library/mac/documentation/Cocoa/Referenc‌​e/…
    • sarah
      sarah over 8 years
      @Darren i already do that but that has nothing to do with the question that is why i didn't show you the code for it
    • Darren
      Darren over 8 years
      You can configure a date formatter to print just the full day name for a given NSDate.
    • Leo Dabus
      Leo Dabus over 8 years
    • Joshua Dance
      Joshua Dance about 6 years
      This is not a duplicate. The other question is more broad. This is the best question for this specific question and answer.
    • Leo
      Leo almost 6 years
      I agree with Joshua that this is not a duplicate question
  • sarah
    sarah over 8 years
    it is a good option, but the array contains ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] not full name for days like i want
  • Leo
    Leo over 8 years
    With shortWeekdaySymbols ,you can get short String.Like the code I update
  • sarah
    sarah over 8 years
    how did you know that weekdaysSymbols contain the full names? i printed it, both of them contain the short name. +1 for the efforts
  • Leo
    Leo over 8 years
    The weekdaysSymbols just works well on my playground. You can also use dateFormatter.Like the code I update
  • Joshua Dance
    Joshua Dance about 6 years
    In Swift 4 the stringFromDate is now formatter.string(from: now)
  • Teodor Ciuraru
    Teodor Ciuraru over 5 years
    Yes, but right after you set the calendar.locale.
  • Iulian Onofrei
    Iulian Onofrei over 5 years
    Make sure you also set the locale for the dateFormatter if you care about i18n.
  • app4g
    app4g over 2 years
    From nsdateformatter.com There isn't an EE option. EEEEE gets MTWTFSS and EEEEEE gets MoTuWeThFrSaSu