NSDate オブジェクトがあり、「月」、「火」、「水」などの短い 3 文字のスタイルで曜日を示すラベルを作成し、任意の言語の短い表現にローカライズする必要があります。
日付フォーマッタ EEE には 3 文字ありますが、おそらく 1 文字しかない中国語のような言語にどのように変換されますか?
NSDate オブジェクトがあり、「月」、「火」、「水」などの短い 3 文字のスタイルで曜日を示すラベルを作成し、任意の言語の短い表現にローカライズする必要があります。
日付フォーマッタ EEE には 3 文字ありますが、おそらく 1 文字しかない中国語のような言語にどのように変換されますか?
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE"];
NSString *strDate = [formatter stringFromDate:date];
[formatter release];
//Commented out the following line as weekday can be of one letter (requirement changed)
//strDate = [strDate substringToIndex:3]; //This way Monday = Mon, Tuesday = Tue
正しいロケールを使用するには、メソッドを使用するdateFormat
インスタンスにを設定します。NSDateFormatter
+ dateFormatFromTemplate:options:locale:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEE" options:0 locale:[NSLocale currentLocale]];
NSLog(@"Weekday using %@: %@", [[NSLocale currentLocale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Test what Chinese will look like without changing device locale in settings
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
NSLog(@"Weekday using zh_CN: %@", [dateFormatter stringFromDate:date]);
これは以下を出力します:
en_US を使用し
た平日: Thu zh_CN を使用した平日: 周四
これにより、必要なものが出力されます。
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
NSString *day = [formatter stringFromDate:date];
[formatter release];
if ([day length] > 3) day = [day substringToIndex:3];
NSLog(@"%@", day);