简体   繁体   English

无论区域设置如何,如何在 Swift 中获取工作日?

[英]How to get weekday in Swift regardless of the locale settings?

In my iOS app user can choose on which day to have certain action active and I am saving each selection as a raw value of OptionSet .在我的 iOS 应用程序中,用户可以选择在哪一天激活某些操作,我将每个选择保存为OptionSet的原始值。 This all works great.这一切都很好。

But I need a way to map current day to the selected options.但我需要一种方法来 map 当天选择的选项。 How can I get some kind of constants for weekdays that do not changed based on whether week starts on Sunday or Monday based on selected locale?如何根据所选区域设置,根据星期是从星期日开始还是星期一开始,工作日的某种常量不会改变?

This:这个:

let weekDay = Calendar.current.component(.weekday, from: Date())

Seems like a solution but the Int constant is going to vary right based on a locale?似乎是一个解决方案,但Int常量会根据语言环境而有所不同? According to very short docs from Apple this should be 1 for Sunday in Gregorian calendar.根据Apple的非常简短的文档,这应该是公历中星期日的1

Is there any way how I can get weekday from Date that will be always same?有什么方法可以让我从Date获得始终相同的工作日?

I would like to map these constants to my OptionSet flags and based on that decide whether action should be active in a given day.我想将这些常量添加到我的OptionSet标志中,并据此决定在给定的一天内是否应该激活操作。

Thanks.谢谢。

You are mixing up weekday and firstWeekday .您正在混淆weekdayfirstWeekday

The weekday component is independent of the locale, in the Gregorian Calendar 1 is always Sunday, 2 Monday etc. weekday组件独立于语言环境,在公历中 1 始终是星期日,2 是星期一等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM