如何在用户的区域设置中获取星期几的名称?

时间:2011-09-07 07:42:41

标签: objective-c ios cocoa-touch localization

我有一个介于1到7之间的数字,我希望将其转换为用户的语言环境,相当于周一到周日。我可以这样做,如果是,怎么做?

7 个答案:

答案 0 :(得分:22)

NSDateFormatter可以为您提供名称列表:

NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale: [NSLocale currentLocale]];
NSArray * weekdays = [df weekdaySymbols];

然后您可以像任何其他数组一样进行索引[weekdays objectAtIndex:dayIdx];但请注意,the first weekday可能因地区而异;究竟如何变化(以及关于NSCalendar的许多其他事情)在文档中没有特别好地解释。

答案 1 :(得分:6)

当然可以。下面是一个方法/函数,它返回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]
}

答案 2 :(得分:4)

这是我的看法:

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"]

答案 3 :(得分:2)

NSArray *weekdaySymbols = [[NSDateFormatter alloc] weekdaySymbols];

您可以使用{weekdaySymbols,shortWeekdaySymbols,veryShortWeekdaySymbols}

之一

答案 4 :(得分:1)

我找到了这个解决方案: 你有你的日期对象,你希望从该对象中获取一周中的某一天。

  1. 将区域设置设置为日期格式化程序,如下所示

    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")
    }
    
  2. 这样做可以确保您获得了日期格式化程序的正确标识符,之后配置了日期格式化程序,您可以继续并获取星期几的字符串

    //convert your date to string
    let stringDate = dateFormatter.string(from: eventDate!)
    
    return stringDate
    
  3. 我希望这有助于某人:)。新年快乐。

答案 5 :(得分:0)

基于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
}

答案 6 :(得分:-1)

如果您正在处理 Date 对象,假设您已将本地化选项添加到您的项目中,您可以轻松使用以下代码:

extension Date {
    var dayName: String {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: Locale.current.identifier)
        dateFormatter.dateFormat  = "E" // "EEEE" to get long style
        return dateFormatter.string(from: self)
    }
}

这将自动检测您应用的语言并将本地化的日期作为字符串输出。