内容简介:翻译自:https://stackoverflow.com/questions/28646629/how-to-get-weekdays-array-from-system-in-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
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 写在本命年最后一个工作日,帝都前端生存指南
- 别人家的 DevOps/运维工程师的工作日(以及周末)这样过
- Java 时间工具包 xk-time 3.0.1 发布,中国工作日计算问题修复
- Java 时间工具包 xk-time 3.0.0 发布,增加时间自然语言分析,中国工作日计算等
- C语言指针数组和数组指针
- 数组 – 如何在Swift中将数组拆分成两半?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。