如何在Swift系统中获取工作日数组?

如何从系统中获取一周中的数组(我认为来自NSDate)?

到现在为止,我只能参加当天的比赛,但我希望能够在阵容中度过所有工作日.

如果将星期的第一天设置为星期一,我的数组将如下所示:

[ Mon, Tue, Wed... ]

如果星期的第一天是星期日,我的数组看起来像:

[Sun, Mon, Tue... ]

码:

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)
试试 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"]

似乎他们总是返回Sun … Sat数组,无论.calendar的.firstWeekday属性.因此,您必须手动旋转它.

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"]

翻译自:https://stackoverflow.com/questions/28646629/how-to-get-weekdays-array-from-system-in-swift