58

I have looked around a bit, but didnt find a quick answer for this in swift 3. I get todays weekday like this:

let weekday = Calendar.current.component(.weekday, from: Date())

But instead, i would like to get the weekday of a given date. How is this done? Thanx

matt
  • 515,959
  • 87
  • 875
  • 1,141
Martin Vidic
  • 763
  • 1
  • 6
  • 19

6 Answers6

100

Presumably you have a Date already?

let weekday = Calendar.current.component(.weekday, from: myDate)

Maybe you want the name of the weekday?

let f = DateFormatter()

f.weekdaySymbols[Calendar.current.component(.weekday, from: Date()) - 1]

The -1 is added at the end because Calendar.current.component(.weekday, from: Date()) returns values from 1-7 but weekdaySymbols expects array indices.

Investigate the NSCalendar/DateComponents api or edit your question to be more specific if you need help with the DateComponents api.

Manuel
  • 14,274
  • 6
  • 57
  • 130
Joe Daniels
  • 1,646
  • 1
  • 11
  • 9
  • 11
    Using `Calendar` as `.iso8601` and an UTC Date the index which I got from my Calendar needs to be subtracted by 1. When the Date was Sunday I got an Index out of range at `f.shortWeekdaySymbols[dayIndex]` because my Day Index was `7` – Marcel T Jan 04 '18 at 09:18
  • 4
    Right, -1 to be within array bounds. – SoftDesigner Feb 08 '18 at 10:51
  • 1
    i think there is something wrong here, i think weekdays always start with Monday in `weekdaySymbols`, because in Sunday i have weekday component (int) = 1, receiving Monday as a result. Calendar.current is gregorian London Time – Stoyan Oct 21 '18 at 04:15
61
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy" // OR "dd-MM-yyyy"

let currentDateString: String = dateFormatter.string(from: date)
print("Current date is \(currentDateString)")
   

Current date is Monday, June 29, 2017

Yakup Ad
  • 1,591
  • 16
  • 13
  • 2
    dateFormat is set twice here. What about other languages? How would you handle the different date formats displayed in different languages e.g. 01/25/2018 vs. 25.01.2018? – palme Aug 06 '18 at 10:28
  • 1
    Upvote this answer because i found't searching for a dateFormat for weekday. But the @joedaniels it's the correct answer for the question – Romulo BM Jun 05 '19 at 13:27
26

Swift5 get week day from DateFormatter

 func getTodayWeekDay()-> String{
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        let weekDay = dateFormatter.string(from: Date())
        return weekDay
  }

 //eg: // "EEEE" --> "Friday" 
       // "EEE"  --> "Fri"
Sukh
  • 1,278
  • 11
  • 19
3

To get local format you can use:

extension Date{

func stringValueFullWithTime()-> String?{
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .full
    dateFormatter.timeStyle = . short
    dateFormatter.locale = Locale.current
    return dateFormatter.string(from: self)
}

This will print as german local:
Sonntag, 11. November 2018 um 11:17

Yedy
  • 2,107
  • 1
  • 25
  • 30
  • `dateFormatter.locale = Locale.current` did not work for me. I had to specify the correct local using `Locale(identifier: "de_DE")`. – WalterBeiter Oct 23 '19 at 09:21
3

If you want dayNumber component in TimeZone

func dayNumberOfWeek() -> Int? {
    let timeZone = TimeZone(abbreviation: "EDT")
    let component =  Calendar.current.dateComponents(in: timeZone!, from: self)
    return  component.day
}
Devesh.452
  • 893
  • 9
  • 10
2

Weekday by default consider first weekday as Sunday, if you wanna support generic context use this extension.

extension Date {

    var calendar: Calendar { Calendar.current }

    var weekday: Int {
        (calendar.component(.weekday, from: self) - calendar.firstWeekday + 7) % 7 + 1
    }
}