如何在 Cocoa 中获取周末与工作日?
How to get weekends vs weekdays in Cocoa?
获得一周中的标准工作日(例如工作日)与一周中的非工作日的正确日历不可知方法是什么(在 iOS 或 OS X 中)?这是考虑到 some parts of the world doesn't follow the Monday through Friday working day 例程。例如。阿联酋和马来西亚部分地区从周日到周四工作。
我想模拟 iOS' 内置闹钟应用程序,如果我在其中 select 周一到周五重复一天,它会说我的闹钟重复 "Weekdays" 而对于在周六和周日触发的警报则表示 "Weekends"。但是,我希望我的应用程序也能在世界其他不遵循西方世界工作周的地方正常运行。
我一直在研究 NSCalendar
的 isDateInWeekend
函数和其他各种日历,但未能找到确定工作日与周末的好方法。这是我目前从 Swift 游乐场获得的片段:
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
我要找的是 returns false
在世界上某些地区实行周一至周五工作日但 returns true
阿联酋等
我终于明白了。错误出在语言环境标识符中,应该是 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
获得一周中的标准工作日(例如工作日)与一周中的非工作日的正确日历不可知方法是什么(在 iOS 或 OS X 中)?这是考虑到 some parts of the world doesn't follow the Monday through Friday working day 例程。例如。阿联酋和马来西亚部分地区从周日到周四工作。
我想模拟 iOS' 内置闹钟应用程序,如果我在其中 select 周一到周五重复一天,它会说我的闹钟重复 "Weekdays" 而对于在周六和周日触发的警报则表示 "Weekends"。但是,我希望我的应用程序也能在世界其他不遵循西方世界工作周的地方正常运行。
我一直在研究 NSCalendar
的 isDateInWeekend
函数和其他各种日历,但未能找到确定工作日与周末的好方法。这是我目前从 Swift 游乐场获得的片段:
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
我要找的是 returns false
在世界上某些地区实行周一至周五工作日但 returns true
阿联酋等
我终于明白了。错误出在语言环境标识符中,应该是 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