35

I compute 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 fine but I was wondering if Swift comes with some libraries to do that automatically.

Cristik
  • 30,989
  • 25
  • 91
  • 127
sarah
  • 1,201
  • 1
  • 9
  • 29
  • You should use NSDateFormatter with a custom date format. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/occ/instp/NSDateFormatter/dateFormat – Darren Nov 15 '15 at 12:29
  • @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 – sarah Nov 15 '15 at 12:35
  • You can configure a date formatter to print just the full day name for a given NSDate. – Darren Nov 15 '15 at 12:42
  • http://stackoverflow.com/a/27369380/2303865 – Leo Dabus Nov 15 '15 at 13:05
  • This is not a duplicate. The other question is more broad. This is the best question for this specific question and answer. – Joshua Dance Mar 17 '18 at 06:06
  • I agree with Joshua that this is not a duplicate question – Leo Jun 17 '18 at 13:40

4 Answers4

108

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

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Leo
  • 24,596
  • 11
  • 71
  • 92
  • it is a good option, but the array contains `["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]` not full name for days like i want – sarah Nov 15 '15 at 12:32
  • With `shortWeekdaySymbols `,you can get short String.Like the code I update – Leo Nov 15 '15 at 12:35
  • how did you know that weekdaysSymbols contain the full names? i printed it, both of them contain the short name. +1 for the efforts – sarah Nov 15 '15 at 12:47
  • The `weekdaysSymbols` just works well on my playground. You can also use dateFormatter.Like the code I update – Leo Nov 15 '15 at 12:53
  • In Swift 4 the stringFromDate is now `formatter.string(from: now)` – Joshua Dance Mar 17 '18 at 06:06
  • Make sure you also set the `locale` for the `dateFormatter` if you care about i18n. – Iulian Onofrei Jan 17 '19 at 11:07
  • From https://nsdateformatter.com There isn't an ```EE``` option. ````EEEEE```` gets ```MTWTFSS``` and ```EEEEEE``` gets ```MoTuWeThFrSaSu``` – app4g Oct 21 '21 at 02:59
21

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
ober
  • 2,363
  • 1
  • 19
  • 17
4

There's a new api since iOS 15.

@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension FormatStyle where Self == Date.FormatStyle {

    public static var dateTime: Date.FormatStyle { get }
}

You can now use:

Date().formatted(.dateTime.month(.wide)) // October
Date().formatted(.dateTime.year(.defaultDigits)) // 2022
Date().formatted(.dateTime.weekday(.wide)) // Thursday
Date().formatted(.dateTime.weekday(.abbreviated)) // Thu
Date().formatted(.dateTime.weekday(.narrow)) // T

and so on and so on

Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
1

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

vpoltave
  • 1,612
  • 3
  • 14
  • 31