How to get weekdays array from system in Swift?
Asked Answered
W

4

20

How can I take an array with days of week from the system (from NSDate, I think)?

Until now, I can only take the current day, but I'd like to be able to take all weekdays in an array.

If the first day of week is set to Monday, my array would look like:

[ Mon, Tue, Wed... ]

If the first day of week is Sunday, my array would look like:

[Sun, Mon, Tue... ]

Code:

let dateNow = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | .CalendarUnitYear , fromDate: dateNow) 

/*This is the way how i take system time */ 

let format = NSDateFormatter()
format.dateFormat = "EEE"
stringDay = format.stringFromDate(dateNow) 
Waddington answered 21/2, 2015 at 13:59 Comment(0)
D
54

Try these properties:

let fmt = NSDateFormatter()
fmt.weekdaySymbols // -> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
fmt.shortWeekdaySymbols // -> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
fmt.veryShortWeekdaySymbols // -> ["S", "M", "T", "W", "T", "F", "S"]
fmt.standaloneWeekdaySymbols // -> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
fmt.shortStandaloneWeekdaySymbols // -> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
fmt.veryShortStandaloneWeekdaySymbols // -> ["S", "M", "T", "W", "T", "F", "S"]

It seems they always return Sun ... Sat array regardless .firstWeekday property of the .calendar. So, you have to rotate it manually.

let firstWeekday = 2 // -> Monday

var symbols = fmt.shortWeekdaySymbols
symbols = Array(symbols[firstWeekday-1..<symbols.count]) + symbols[0..<firstWeekday-1]
// -> ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
Dementia answered 21/2, 2015 at 14:29 Comment(1)
You can also get the current locale's first weekday with Calendar.current.firstWeekDay in later Swift versions instead of providing it manually.Daviddavida
C
1

Swift

Create a DateFormatter or a Calendar and get the desired array like::

let formatter = DateFormatter()

// 💡 You can setup the formatter here. For example, setting the locale to return the weekdays in the desired language.
formatter.locale = .init(identifier: "fa")

formatter.weekdaySymbols \\ ["یکشنبه", "دوشنبه", "سه‌شنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه"]
formatter.veryShortWeekdaySymbols \\ ["ی", "د", "س", "چ", "پ", "ج", "ش"]

Note Creating a formatter is a very expensive task and you may experience lags in the main thread. Try caching it if you feel you will need it again

Note that you can use en identifier to get all above in English like:

formatter.locale = .init(identifier: "en")

formatter.weekdaySymbols // -> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
formatter.shortWeekdaySymbols // -> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
formatter.veryShortWeekdaySymbols // -> ["S", "M", "T", "W", "T", "F", "S"]
Correspond answered 22/7, 2021 at 13:29 Comment(0)
E
1

Now we can use Calendar.current instead of DateFormatter:

    Calendar.current.weekdaySymbols // -> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    Calendar.current.shortWeekdaySymbols // -> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    Calendar.current.veryShortWeekdaySymbols // -> ["S", "M", "T", "W", "T", "F", "S"]
    Calendar.current.standaloneWeekdaySymbols // -> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    Calendar.current.shortStandaloneWeekdaySymbols // -> ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    Calendar.current.veryShortStandaloneWeekdaySymbols // -> ["S", "M", "T", "W", "T", "F", "S"]
Ergosterol answered 6/2, 2023 at 10:2 Comment(0)
C
-4

If you want to have Monday as the first day, you could use this extension.

extension SequenceType where Generator.Element == String {
    func mondayFirst(withSunday: (Bool)) -> [String] {
        var tempWeek = self as! [String]

        let tempDay = tempWeek.first

        tempWeek.removeFirst()

        if (!withSunday) {
            tempWeek.append(tempDay!)
        }

        return tempWeek
    }
}
Chops answered 3/12, 2015 at 12:55 Comment(1)
Extending SequenceType with calendar-related functionality is not a good idea.Syce

© 2022 - 2024 — McMap. All rights reserved.