作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
获取一周中的标准工作日(例如工作日)与一周中的非工作日的正确的与日历无关的方式(在 iOS 或 OS X 中)是什么?这是考虑到 some parts of the world doesn't follow the Monday through Friday working day常规。例如。阿联酋和马来西亚部分地区的工作时间为周日至周四。
我希望模拟 iOS 的内置闹钟应用程序,如果我选择周一到周五的重复日期,它会说我的闹钟重复“工作日”,而对于在周六和周日触发的闹钟则显示“周末”。不过,我希望我的应用程序也能在不遵循西方世界工作周的世界其他地区正常工作。
我一直在尝试使用 NSCalendar
的 isDateInWeekend
函数和其他各种日历,但一直无法找到确定工作日与周末的好方法。这是我迄今为止得到的 Swift Playground 的一个片段:
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
,但对于阿联酋等地区返回 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
关于cocoa - 如何在 cocoa 度过周末和工作日?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30277494/
我是一名优秀的程序员,十分优秀!