2

NSDate オブジェクトがあり、「月」、「火」、「水」などの短い 3 文字のスタイルで曜日を示すラベルを作成し、任意の言語の短い表現にローカライズする必要があります。

日付フォーマッタ EEE には 3 文字ありますが、おそらく 1 文字しかない中国語のような言語にどのように変換されますか?

4

3 に答える 3

8
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
于 2012-09-04T09:08:40.690 に答える
4

正しいロケールを使用するには、メソッドを使用する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 を使用した平日: 周四

于 2012-09-06T21:59:06.057 に答える
1

これにより、必要なものが出力されます。

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);
于 2012-09-04T09:03:08.353 に答える