114

I created a method that is supposed to take in a string in "YYYY-MM-DD" form and spit out an int that represents the dates position in relation to the week it is in (regardless if it overlaps between months). So e.g sunday=1 monday=2 and so on.

Here is my code:

    func getDayOfWeek(today:String)->Int{

    var formatter:NSDateFormatter = NSDateFormatter()
    formatter.dateFormat = "YYYY-MM-DD"
    var todayDate:NSDate = formatter.dateFromString(today)!
    var myCalendar:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
    var myComponents = myCalendar.components(NSCalendarUnit.WeekdayOrdinalCalendarUnit, fromDate: todayDate)
    var weekDay = myComponents.weekdayOrdinal
    return weekDay
}

I know that NSCalendarUnit.WeekdayOrdinalCalendar is wrong but I have tried I think most logical combinations. And have also messed around with myComponents.weekdayOrdinal e.g used mycomponents.day or .weekday.

Here are my options in what to use:

static var EraCalendarUnit: NSCalendarUnit { get }
static var YearCalendarUnit: NSCalendarUnit { get }
static var MonthCalendarUnit: NSCalendarUnit { get }
static var DayCalendarUnit: NSCalendarUnit { get }
static var HourCalendarUnit: NSCalendarUnit { get }
static var MinuteCalendarUnit: NSCalendarUnit { get }
static var SecondCalendarUnit: NSCalendarUnit { get }
static var WeekCalendarUnit: NSCalendarUnit { get }
static var WeekdayCalendarUnit: NSCalendarUnit { get }
static var WeekdayOrdinalCalendarUnit: NSCalendarUnit { get }
static var QuarterCalendarUnit: NSCalendarUnit { get }
static var WeekOfMonthCalendarUnit: NSCalendarUnit { get }
static var WeekOfYearCalendarUnit: NSCalendarUnit { get }
static var YearForWeekOfYearCalendarUnit: NSCalendarUnit { get }
static var CalendarCalendarUnit: NSCalendarUnit { get }
static var TimeZoneCalendarUnit: NSCalendarUnit { get }

It is not clear to me since there is no DayOfWeekUnit option (or something similar).

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
boidkan
  • 4,691
  • 5
  • 29
  • 43
  • 1
    See: [ICU Formatting Dates and Times](http://userguide.icu-project.org/formatparse/datetime) for formatting characters. – zaph Aug 27 '14 at 17:30
  • Possible duplicate of [How do I get the day of the week with Cocoa Touch?](http://stackoverflow.com/questions/4269093/how-do-i-get-the-day-of-the-week-with-cocoa-touch) – Suhaib Oct 05 '16 at 04:03
  • 1
    @Suhaib If you look at my tags it says Swift not Objective-C. Also, the code is in Swift 2. – boidkan Oct 05 '16 at 15:27
  • try the following "EEEE" in your dateformatter – Khaledonia Jun 13 '18 at 13:11

21 Answers21

184

Swift 3 & 4

Retrieving the day of the week's number is dramatically simplified in Swift 3 because DateComponents is no longer optional. Here it is as an extension:

extension Date {
    func dayNumberOfWeek() -> Int? {
        return Calendar.current.dateComponents([.weekday], from: self).weekday 
    }
}

// returns an integer from 1 - 7, with 1 being Sunday and 7 being Saturday
print(Date().dayNumberOfWeek()!) // 4

If you were looking for the written, localized version of the day of week:

extension Date {
    func dayOfWeek() -> String? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        return dateFormatter.string(from: self).capitalized 
        // or use capitalized(with: locale) if you want
    }
}

print(Date().dayOfWeek()!) // Wednesday
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • 1
    Maybe it would be more clear if you put aDate.dayOfWeek() under Usage. – boidkan Jan 26 '16 at 18:20
  • 1
    Note that the question was how to get the weekday from a *string* in the form "YYYY-MM-DD". Of course it simplifies if you already have an NSDate. – Martin R May 27 '16 at 13:57
  • 6
    It's probably worth to mention that if you're using it in eg. table view, it may slow down the scrolling, as creating DateFormatter is quite costly. If you're going to use it very often, it's good to eg. create one instance and provide it during calculation to all of the function calls. – Nat Jan 25 '17 at 00:48
  • Absolutely. In that case, even have an array handler where you could run the function on an array of data would be useful. – brandonscript Jan 25 '17 at 01:07
  • 2
    simpler would be just answer of @david72 below: Calendar.current.component(.weekday, from: Date()) – Adam Smaka Feb 15 '17 at 16:13
  • 2
    What is dateFormat = "EEEE"? I can't find it anywhere in the documentation – J. Doe Aug 22 '18 at 19:52
  • 1
    @J.Doe docs aren't great for `NSDateFormatter`. Try instead: http://nsdateformatter.com – brandonscript Aug 22 '18 at 20:49
  • a good website for the format strings like "EEEE" is : http://userguide.icu-project.org/formatparse/datetime – Hardy_Germany Oct 23 '18 at 19:50
125

What you are looking for (if I understand the question correctly) is NSCalendarUnit.CalendarUnitWeekday. The corresponding property of NSDateComponents is weekday.

Note also that your date format is wrong (the full specification can be found here: http://unicode.org/reports/tr35/tr35-6.html).

The function can be simplified slightly, using automatic type inference, also you use variables a lot where constants are sufficient. In addition, the function should return an optional which is nil for an invalid input string.

Updated code for Swift 3 and later:

func getDayOfWeek(_ today:String) -> Int? {
    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)
    return weekDay
}

Example:

if let weekday = getDayOfWeek("2014-08-27") {
    print(weekday)
} else {
    print("bad input")
}

Original answer for Swift 2:

func getDayOfWeek(today:String)->Int? {

    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    if let todayDate = formatter.dateFromString(today) {
        let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        let myComponents = myCalendar.components(.Weekday, fromDate: todayDate)
        let weekDay = myComponents.weekday
        return weekDay
    } else {
        return nil
    }
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Yes it was the format is what was messing me up. I was getting 2 instead of 4 when I tried WeekdayCalendarUnit and was very confused! Thanks for the link, I will read up on it. – boidkan Aug 27 '14 at 17:29
  • 1
    I know it's not part of the original question, but this would be a good place to return an optional Int in the case that the passed in date is not in the correct format or invalid in some way. – Mike S Aug 27 '14 at 17:30
  • 1
    Shouldn't it be `func getDayOfWeek(today:String)->Int?`. If you force unwrap the optional it still crashes, doesn't it? – HAS Aug 27 '14 at 17:47
  • 1
    @HAS: You are completely right, thanks! - It did not crash in my test case because the optional assignment `if let ...` works also with implicitly unwrapped optionals. – Martin R Aug 27 '14 at 17:50
  • .WeekdayCalendarUnit will be replaced with ```.Weekday``` in iOS 8. – Shmidt Aug 27 '14 at 17:57
  • Martin R ... Update for Swift 2 is: Replace .CalendarUnitWeekday with .Weekday The "d" in .Weekday is lowercase – Eric Sep 19 '15 at 23:02
  • 1
    Thank you Marting For this, To make your answer more beautiful you could edit it to remove the swift 2 code and keep the swift 3 code. Then add a note at the end saying: "If you need code for swift 2, check the previous edit " or something like that – Suhaib Oct 05 '16 at 04:01
  • @PrasadPatil: Thank you for the edit suggestion. But note that the Swift 3 version already defines the function with an empty argument label, so it can be called as `getDayOfWeek("2014-08-27")` – Martin R Apr 26 '18 at 10:42
43

If you want the full "Sunday", "Monday", "Tuesday", "Wednesday" etc.

EDIT: There's actually a built in format that returns localized day names:

extension NSDate {
    func dayOfTheWeek() -> String? {        
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "EEEE"
        return dateFormatter.stringFromDate(self)
    }
}

My previous solution (for English only):

extension NSDate {

    func dayOfTheWeek() -> String? {
        let weekdays = [
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"
        ]

        let calendar: NSCalendar = NSCalendar.currentCalendar()
        let components: NSDateComponents = calendar.components(.Weekday, fromDate: self)
        return weekdays[components.weekday - 1]
    }
}

You don't need to unwrap calendar and components, they are guaranteed by the foundation framework.

Usage:

print(myDate.dayOfTheWeek())
Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
Matjan
  • 3,591
  • 1
  • 33
  • 31
31

The simple answer (swift 3):

Calendar.current.component(.weekday, from: Date())
david72
  • 7,151
  • 3
  • 37
  • 59
12

In my case I was after a three letter string for each day. I modified @Martin R's function as follows:

func getDayOfWeekString(today:String)->String? {
    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    if let todayDate = formatter.dateFromString(today) {
        let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        let myComponents = myCalendar.components(.Weekday, fromDate: todayDate)
        let weekDay = myComponents.weekday
        switch weekDay {
        case 1:
            return "Sun"
        case 2:
            return "Mon"
        case 3:
            return "Tue"
        case 4:
            return "Wed"
        case 5:
            return "Thu"
        case 6:
            return "Fri"
        case 7:
            return "Sat"
        default:
            print("Error fetching days")
            return "Day"
        }
    } else {
        return nil
    }
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Tr0yJ
  • 3,274
  • 1
  • 22
  • 30
12
extension Date {

var weekdayName: String {
    let formatter = DateFormatter(); formatter.dateFormat = "E"
    return formatter.string(from: self as Date)
}

var weekdayNameFull: String {
    let formatter = DateFormatter(); formatter.dateFormat = "EEEE"
    return formatter.string(from: self as Date)
}
var monthName: String {
    let formatter = DateFormatter(); formatter.dateFormat = "MMM"
    return formatter.string(from: self as Date)
}
var OnlyYear: String {
    let formatter = DateFormatter(); formatter.dateFormat = "YYYY"
    return formatter.string(from: self as Date)
}
var period: String {
    let formatter = DateFormatter(); formatter.dateFormat = "a"
    return formatter.string(from: self as Date)
}
var timeOnly: String {
    let formatter = DateFormatter(); formatter.dateFormat = "hh : mm"
    return formatter.string(from: self as Date)
}
var timeWithPeriod: String {
    let formatter = DateFormatter(); formatter.dateFormat = "hh : mm a"
    return formatter.string(from: self as Date)
}

var DatewithMonth: String {
    let formatter = DateFormatter(); formatter.dateStyle = .medium ;        return formatter.string(from: self as Date)
}
}

usage let weekday = Date().weekdayName

Akash Shindhe
  • 558
  • 4
  • 16
11

There is an easier way. Just pass your string date to the following function, it will give you the day name :)

func getDayNameBy(stringDate: String) -> String
    {
        let df  = NSDateFormatter()
        df.dateFormat = "YYYY-MM-dd"
        let date = df.dateFromString(stringDate)!
        df.dateFormat = "EEEE"
        return df.stringFromDate(date);
    }
Hamid
  • 2,852
  • 1
  • 28
  • 45
10

SWIFT 5 - Day of the week

    extension Date {

        var dayofTheWeek: String {
             let dayNumber = Calendar.current.component(.weekday, from: self)
             // day number starts from 1 but array count from 0
             return daysOfTheWeek[dayNumber - 1]
        }

        private var daysOfTheWeek: [String] {
             return  ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
        }
     }

Date extension based on Fattie's answer

Dileep
  • 2,399
  • 4
  • 26
  • 39
9

Swift 3 Date extension

extension Date {
    var weekdayOrdinal: Int {
        return Calendar.current.component(.weekday, from: self)
    }
}
quemeful
  • 9,542
  • 4
  • 60
  • 69
7

You can use this table date_formats for converting your date to different formats. My shortest code:

func getDayOfWeek(today: String) -> Int{
    let formatter:NSDateFormatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    let todayDate = formatter.dateFromString(today)
    formatter.dateFormat = "e" // "eeee" -> Friday
    let weekDay = formatter.stringFromDate(todayDate!)
    return Int(weekDay)!
}
getDayOfWeek("2015-12-18") // 6
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
qwerty-reloader
  • 151
  • 2
  • 8
5

There are already a lot of answers here but I think there's another, perhaps better, way of doing this using the correct Calendar APIs.

I'd suggest getting the day of the week using the weekdaySymbols property of Calendar (docs) in an extension to Date:

extension Date {

    /// Returns the day of the week as a `String`, e.g. "Monday"
    var dayOfWeek: String {
        let calendar = Calendar.autoupdatingCurrent
        return calendar.weekdaySymbols[calendar.component(.weekday, from: self) - 1]
    }
}

This requires initialising a Date first, which I would do using a custom DateFormatter:

extension DateFormatter {

    /// returns a `DateFormatter` with the format "yyyy-MM-dd".
    static var standardDate: DateFormatter {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }
}

This can then be called with:

DateFormatter.standardDate.date(from: "2018-09-18")!.dayOfWeek

Why I prefer this:

  1. dayOfWeek does not have to care about time zones because the user's calendar is used, some of the other solutions here will show the incorrect day because time zones are not considered.
  2. It's very likely that you'll need to use dates in the same format in other places so why not create a reusable DateFormatter and use that instead?
  3. weekdaySymbols is localised for you.
  4. weekDaySymbols can be replaced with other options such as shortWeekdaySymbols for "Mon", "Tues" etc.

Please note: This example DateFormatter also doesn't consider time zones or locales, you'll need to set them for what you need. If the dates are always precise, consider setting the time zone TimeZone(secondsFromGMT: 0).

Leon
  • 3,614
  • 1
  • 33
  • 46
3

The practical solution ...

Be aware that results are the integers one through seven.

(Not zero through six.)

let trivialDayStringsORDINAL = ["", "SUN","MON","TUE","WED","THU","FRI","SAT"]
// note that zero is not used

and then ...

let dow = Calendar.current.component(.weekday, from: someDate)
print( trivialDayStringsORDINAL[dow] )
Fattie
  • 27,874
  • 70
  • 431
  • 719
3
extension Date {

    enum Weekday: CaseIterable {
        case sunday
        case monday
        case tuesday
        case wednesday
        case thursday
        case friday
        case saturday
    }
    
    var isWednesday: Bool { dayOfTheWeek == .wednesday } // 
        
    var dayOfTheWeek: Date.Weekday {
        let dayNumber = Calendar.current.component(.weekday, from: self)
        return Weekday.allCases[dayNumber - 1]
    }
 }
Nico S.
  • 3,056
  • 1
  • 30
  • 64
2

SWIFT 5

let MyFormatter = DateFormatter()
MyFormatter.dateFormat = "EE MMM dd"
print(MyFormatter.string(from: Date()))

Output is: Thu Jun 03

One can add to the number of E's or M's to change the format of day or month. YY or YYYY can be added for the year as well.

Koko
  • 21
  • 1
  • 4
1

I ended up needing a few more strings from the date, including date of the week (e.g. "5th") and month of the year (e.g. Aug). Below are all three functions I have created based upon @Martin R's function and modified to return 3 char strings:

//Date String Helper Functions
func getDayOfWeek(today:String)->String? {
    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    if let todayDate = formatter.dateFromString(today) {
        let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        let myComponents = myCalendar.components(.Weekday, fromDate: todayDate)
        let weekDay = myComponents.weekday
        switch weekDay {
        case 1:
            return "Sun"
        case 2:
            return "Mon"
        case 3:
            return "Tue"
        case 4:
            return "Wed"
        case 5:
            return "Thu"
        case 6:
            return "Fri"
        case 7:
            return "Sat"
        default:
            print("Error fetching days")
            return "Day"
        }
    } else {
        return nil
    }
}

func getDateOfMonth(today:String)->String? {
    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    if let todayDate = formatter.dateFromString(today) {
        let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        let myComponents = myCalendar.components(.Day, fromDate: todayDate)
        let weekDay = myComponents.day
        switch weekDay {
        case 1:
            return "1st"
        case 2:
            return "2nd"
        case 3:
            return "3rd"
        case 4:
            return "4th"
        case 5:
            return "5th"
        case 6:
            return "6th"
        case 7:
            return "7th"
        case 8:
            return "8th"
        case 9:
            return "9th"
        case 10:
            return "10th"
        case 11:
            return "11th"
        case 12:
            return "12th"
        case 13:
            return "13th"
        case 14:
            return "14th"
        case 15:
            return "15th"
        case 16:
            return "16th"
        case 17:
            return "17th"
        case 18:
            return "18th"
        case 19:
            return "19th"
        case 20:
            return "20th"
        case 21:
            return "21st"
        case 22:
            return "22nd"
        case 23:
            return "23rd"
        case 24:
            return "24th"
        case 25:
            return "25th"
        case 26:
            return "26th"
        case 27:
            return "27th"
        case 28:
            return "28th"
        case 29:
            return "29th"
        case 30:
            return "30th"
        case 31:
            return "31st"
        default:
            print("Error fetching Date Of Month")
            return "Day"
        }
    } else {
        return nil
    }
}

func getMonthOfYear(today:String)->String? {
    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    if let todayDate = formatter.dateFromString(today) {
        let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        let myComponents = myCalendar.components(.Month, fromDate: todayDate)
        let month = myComponents.month
        switch month {
        case 1:
            return "Jan"
        case 2:
            return "Feb"
        case 3:
            return "Mar"
        case 4:
            return "Apr"
        case 5:
            return "May"
        case 6:
            return "Jun"
        case 7:
            return "Jul"
        case 8:
            return "Aug"
        case 9:
            return "Sep"
        case 10:
            return "Oct"
        case 11:
            return "Nov"
        case 12:
            return "Dec"
        default:
            print("Error fetching months")
            return "Month"
        }
    } else {
        return nil
    }
}
Tr0yJ
  • 3,274
  • 1
  • 22
  • 30
1

SWIFT 2.0 code to present the current week starting from monday.

@IBAction func show(sender: AnyObject) {

   // Getting Days of week corresponding to their dateFormat

    let calendar = NSCalendar.currentCalendar()
    let dayInt: Int!
    var weekDate: [String] = []
    var i = 2

    print("Dates corresponding to days are")
    while((dayInt - dayInt) + i < 9)
    {
        let weekFirstDate = calendar.dateByAddingUnit(.Day, value: (-dayInt+i), toDate: NSDate(), options: [])
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "EEEE dd MMMM"
        let dayOfWeekString = dateFormatter.stringFromDate(weekFirstDate!)
        weekDate.append(dayOfWeekString)
        i++
    }
    for i in weekDate
    {
        print(i) //Printing the day stored in array
    }
}

// function to get week day
func getDayOfWeek(today:String)->Int {

    let formatter  = NSDateFormatter()
    formatter.dateFormat = "MMM-dd-yyyy"
    let todayDate = formatter.dateFromString(today)!
    let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let myComponents = myCalendar.components(.Weekday, fromDate: todayDate)
    let weekDay = myComponents.weekday
    return weekDay
}


@IBAction func DateTitle(sender: AnyObject) {


    // Getting currentDate and weekDay corresponding to it
    let currentDate = NSDate()
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MMM-dd-yyyy"
    let dayOfWeekStrings = dateFormatter.stringFromDate(currentDate)
    dayInt = getDayOfWeek(dayOfWeekStrings)

}
Himanshu
  • 2,832
  • 4
  • 23
  • 51
1

This code is to find the whole current week. It is written in Swift 2.0 :

var i = 2
var weekday: [String] = []
var weekdate: [String] = []
var weekmonth: [String] = []

@IBAction func buttonaction(sender: AnyObject) {

    let currentDate = NSDate()
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MMM-dd-yyyy"
    let dayOfWeekStrings = dateFormatter.stringFromDate(currentDate)
    let weekdays = getDayOfWeek(dayOfWeekStrings)
    let calendar = NSCalendar.currentCalendar()

    while((weekdays - weekdays) + i < 9)
    {
        let weekFirstDate = calendar.dateByAddingUnit(.Day, value: (-weekdays+i), toDate: NSDate(), options: [])

        let dayFormatter = NSDateFormatter()
        dayFormatter.dateFormat = "EEEE"
        let dayOfWeekString = dayFormatter.stringFromDate(weekFirstDate!)
        weekday.append(dayOfWeekString)

        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "dd"
        let dateOfWeekString = dateFormatter.stringFromDate(weekFirstDate!)
        weekdate.append(dateOfWeekString)

        let monthFormatter = NSDateFormatter()
        monthFormatter.dateFormat = "MMMM"
        let monthOfWeekString = monthFormatter.stringFromDate(weekFirstDate!)
        weekmonth.append(monthOfWeekString)

        i++
    }

    for(var j = 0; j<7 ; j++)
    {
    let day = weekday[j]
    let date = weekdate[j]
    let month = weekmonth[j]
    var wholeweek = date + "-" + month + "(" + day + ")"
    print(wholeweek)
    }

}

func getDayOfWeek(today:String)->Int {
    let formatter  = NSDateFormatter()
    formatter.dateFormat = "MMM-dd-yyyy"
    let todayDate = formatter.dateFromString(today)!
    let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let myComponents = myCalendar.components(.Weekday, fromDate: todayDate)
    let daynumber = myComponents.weekday
    return daynumber
}

The output will be like this:

14March(Monday) 15March(Tuesday) 16March(Wednesday) 17March(Thursday) 18March(Friday) 19March(Saturday) 20March(Sunday)

0

For Swift4 to get weekday from string

   func getDayOfWeek(today:String)->Int {
        let formatter  = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        let todayDate = formatter.date(from: today)!
        let myCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
        let myComponents = myCalendar.components(NSCalendar.Unit.weekday, from: todayDate)
        let weekDay = myComponents.weekday
        return weekDay!
    }

let weekday = getDayOfWeek(today: "2018-10-10")
print(weekday) // 4
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
Sébastien REMY
  • 2,399
  • 21
  • 39
0

Swift 3 : Xcode 8 helper function:

func getDayOfWeek(fromDate date: Date) -> String? {
    let cal = Calendar(identifier: .gregorian)
    let dayOfWeek = cal.component(.weekday, from: date)
    switch dayOfWeek {
     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 nil
    }
}
aBikis
  • 323
  • 4
  • 16
0

If you want to take full text use below code:

func dayOfWeek(date: Date()) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "EEEE"
    return dateFormatter.string(from: date).capitalized 
}


print(Date().dayOfWeek()!) // Monday

Or you want short text, take this code:

func dayOfWeek(date: Date()) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "EEE"
    return dateFormatter.string(from: date).capitalized 
}


print(Date().dayOfWeek()!) // Mon
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
0

This version returns a computed property and should be called after setting the locale if needed...

extension Date {
  /**
   Usage: print(Date().dayOfWeek) \\\ Monday
   */
  var dayOfWeek: String {
    let formatter = DateFormatter()
    formatter.setLocalizedDateFormatFromTemplate("EEEE")
    return formatter.string(from: self)
  }
}
vmanjz
  • 1,156
  • 1
  • 10
  • 23