如何在Cocoa中获得周末与工作日?

时间:2015-05-16 15:31:34

标签: cocoa locale nsdateformatter nscalendar

正确的日历无关方式(在iOS或OS X中)获得一周中的标准工作日(例如工作日)与一周中的非工作日有什么关系?这考虑了some parts of the world doesn't follow the Monday through Friday working day例程。例如。阿联酋和马来西亚的一些地方从周日到周四工作。

我希望效仿iOS'内置报警应用程序,如果我选择星期一到星期五的重复日,它会说我的报警重复并且#34;工作日"而对于星期六和星期日发射的警报说" Weekends"。但是,我希望我的应用程序能够在世界其他地方无法正常工作,并且不会跟随西方世界的工作周。

我一直在使用NSCalendar的{​​{1}}功能与各种其他日历一起玩弄,并且找不到确定工作日与周末的好方法。这是我到目前为止从Swift游乐场获得的片段:

isDateInWeekend

我正在寻找的东西会在星期五至星期五工作日的周五返回import UIKit let arabicLocale = NSLocale(localeIdentifier: "en_AR") let islamicTabularCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIslamicTabular) islamicTabularCalendar?.locale = arabicLocale let islamicTabularFormatter = NSDateFormatter() islamicTabularFormatter.calendar = islamicTabularCalendar islamicTabularFormatter.dateStyle = NSDateFormatterStyle.FullStyle let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) gregorianCalendar?.locale = arabicLocale let gregorianFormatter = NSDateFormatter() gregorianFormatter.calendar = gregorianCalendar gregorianFormatter.dateStyle = NSDateFormatterStyle.FullStyle let monday = NSDateComponents() monday.weekday = 2 monday.weekOfMonth = 0 let friday = NSDateComponents() friday.weekday = 6 friday.weekOfMonth = 0 let mondayDate = islamicTabularCalendar?.dateFromComponents(monday) gregorianFormatter.stringFromDate(mondayDate!) // "Monday, July 8, 622" islamicTabularFormatter.stringFromDate(mondayDate!) // "Monday, Dhuʻl-Hijjah 20, 0 AH" islamicTabularCalendar?.isDateInWeekend(mondayDate!) // false let fridayDate = islamicTabularCalendar?.dateFromComponents(friday) gregorianFormatter.stringFromDate(fridayDate!) // Friday, July 12, 622 islamicTabularFormatter.stringFromDate(fridayDate!) // Friday, Dhuʻl-Hijjah 24, 0 AH islamicTabularCalendar?.isDateInWeekend(fridayDate!) // false 星期五,但会返回false阿联酋等。

1 个答案:

答案 0 :(得分:0)

我终于明白了。错误在于区域设置标识符,应该是en_AE而不是en_AR

import UIKit

let arabicLocale = NSLocale(localeIdentifier: "en_AE")
let islamicTabularCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIslamicTabular)
islamicTabularCalendar?.locale = arabicLocale
let islamicTabularFormatter = NSDateFormatter()
islamicTabularFormatter.calendar = islamicTabularCalendar
islamicTabularFormatter.dateStyle = NSDateFormatterStyle.FullStyle

let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
gregorianCalendar?.locale = arabicLocale
let gregorianFormatter = NSDateFormatter()
gregorianFormatter.calendar = gregorianCalendar
gregorianFormatter.dateStyle = NSDateFormatterStyle.FullStyle

let monday = NSDateComponents()
monday.weekday = 2
monday.weekOfMonth = 0
let friday = NSDateComponents()
friday.weekday = 6
friday.weekOfMonth = 0

let mondayDate = islamicTabularCalendar?.dateFromComponents(monday)
gregorianFormatter.stringFromDate(mondayDate!)          // "Monday, July 8, 622"
islamicTabularFormatter.stringFromDate(mondayDate!)     // "Monday, Dhuʻl-Hijjah 20, 0 AH"
islamicTabularCalendar?.isDateInWeekend(mondayDate!)    // false
gregorianCalendar?.isDateInWeekend(mondayDate!)         // false

let fridayDate = islamicTabularCalendar?.dateFromComponents(friday)
gregorianFormatter.stringFromDate(fridayDate!)          // Friday, July 12, 622
islamicTabularFormatter.stringFromDate(fridayDate!)     // Friday, Dhuʻl-Hijjah 24, 0 AH
islamicTabularCalendar?.isDateInWeekend(fridayDate!)    // true
gregorianCalendar?.isDateInWeekend(fridayDate!)         // false