gpt4 book ai didi

cocoa - 如何在 cocoa 度过周末和工作日?

转载 作者:行者123 更新时间:2023-12-03 17:20:33 26 4
gpt4 key购买 nike

获取一周中的标准工作日(例如工作日)与一周中的非工作日的正确的与日历无关的方式(在 iOS 或 OS X 中)是什么?这是考虑到 some parts of the world doesn't follow the Monday through Friday working day常规。例如。阿联酋和马来西亚部分地区的工作时间为周日至周四。

我希望模拟 iOS 的内置闹钟应用程序,如果我选择周一到周五的重复日期,它会说我的闹钟重复“工作日”,而对于在周六和周日触发的闹钟则显示“周末”。不过,我希望我的应用程序也能在不遵循西方世界工作周的世界其他地区正常工作。

我一直在尝试使用 NSCalendarisDateInWeekend 函数和其他各种日历,但一直无法找到确定工作日与周末的好方法。这是我迄今为止得到的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com