如何在用户的区域设置中获取星期几的名称?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在用户的区域设置中获取星期几的名称?相关的知识,希望对你有一定的参考价值。
我有一个介于1和7之间的数字,我希望将其转换为用户的语言环境,相当于周一到周日。我可以这样做,如果是这样,怎么样?
答案
NSDateFormatter
可以为您提供名称列表:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale: [NSLocale currentLocale]];
NSArray * weekdays = [df weekdaySymbols];
然后,你可以像任何其他数组一样索引[weekdays objectAtIndex:dayIdx];
但是,请注意,the first weekday可能因地区而异;究竟如何变化(以及关于NSCalendar
的许多其他事情)在文档中没有特别好的解释。
另一答案
当然可以。下面是一个方法/函数,它返回0到6范围内工作日编号的工作日名称。
Objective C版本:
- (NSString *)weekdayNameFromWeekdayNumber:(NSInteger)weekdayNumber
{
NSCalendar *calendar = [NSCalendar currentCalendar];
// Fetch the days of the week in words for the current language (Sunday to Saturday)
NSArray *weekdaySymbols = calendar.weekdaySymbols;
// Because the first week day changes depending on the region settings.
// ie. In Bangladesh the first day of the week is Friday. In UK it is Monday
NSInteger index = (weekdayNumber + calendar.firstWeekday - 1) % 7;
return weekdaySymbols[index];
}
和Swift 2版本:
func weekdayNameFromWeekdayNumber(weekdayNumber: Int) -> String {
let calendar = NSCalendar.currentCalendar()
let weekdaySymbols = calendar.weekdaySymbols
let index = (weekdayNumber + calendar.firstWeekday - 1) % 7
return weekdaySymbols[index]
}
和Swift 3.0版本:
func weekdayNameFrom(weekdayNumber: Int) -> String {
let calendar = Calendar.current
let weekdaySymbols = calendar.weekdaySymbols
let index = (weekdayNumber + calendar.firstWeekday - 1) % 7
return weekdaySymbols[index]
}
另一答案
这是我的看法:
Swift 3,ios8 +
func weekdayNameFrom(weekdayNumber: Int) -> String {
let calendar = Calendar.current
let dayIndex = ((weekdayNumber - 1) + (calendar.firstWeekday - 1)) % 7
return calendar.weekdaySymbols[dayIndex]
}
请注意,您有3个可用列表:
weekdaySymbols
- >["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
shortWeekdaySymbols
- >["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
veryShortWeekdaySymbols
- >["S", "M", "T", "W", "T", "F", "S"]
另一答案
NSArray *weekdaySymbols = [[NSDateFormatter alloc] weekdaySymbols];
您可以使用{weekdaySymbols,shortWeekdaySymbols,veryShortWeekdaySymbols}之一
另一答案
我找到了这个解决方案:你有你的日期对象,你希望得到该对象的一周中的那一天。
- 将语言环境设置为日期格式化程序,如下所示
let eventDate = dateFormatter.date(from: self) let language = Locale.preferredLanguages.first print("The locale id is (language)") // This date format can be what ever you like, in this case i used EE dd dateFormatter.dateFormat = "EE dd" if let idiomaLocale = language { dateFormatter.locale = Locale(identifier: idiomaLocale) } else { dateFormatter.locale = Locale(identifier: "es_CO") }
- 这样做可以确保您获得了日期格式化程序的正确标识符,之后配置了日期格式化程序,您可以继续并获取星期几的字符串
//convert your date to string let stringDate = dateFormatter.string(from: eventDate!) return stringDate
我希望这有助于某人:)。新年快乐。
另一答案
基于gavdotnet优秀的解决方案,欧洲版(从周一开始)是:
func weekdayNameFromWeekdayNumber(weekdayNumber: Int) -> String {
var EuroDay = weekdayNumber + 1
if EuroDay == 7 {
EuroDay = 0
}
let calendar = NSCalendar.currentCalendar()
let weekdaySymbols = calendar.weekdaySymbols
let index = EuroDay + calendar.firstWeekday - 1
return weekdaySymbols[index].uppercaseString
}
以上是关于如何在用户的区域设置中获取星期几的名称?的主要内容,如果未能解决你的问题,请参考以下文章
程序读出一个表示星期几的数字(1-7),输出对应的星期字符串名称?
pandas使用to_datetime函数将字符串时间数据列转化为时间对象数据列通过DatetimeProperties对象获取日期对象在所在周的周几星期几的名称信息(week name)