LoginSignup
23
22

More than 5 years have passed since last update.

日付をローカライズしながらフォーマットも指定するdateFormatFromTemplate:options:locale

Last updated at Posted at 2012-06-28

NSDateFormatterShortStyleを指定したけど12/6/28とか表示されてYearは要らないのにキー!となったらtemplateを試しましょう。日付の要素を指定しつつローカライズしてくれます。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"Md" options:0 locale:[NSLocale currentLocale]]];
NSLog(@"%@", [formatter stringFromDate:[NSDate date]]); // 6/28
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSLog(@"%@", [formatter stringFromDate:[NSDate date]]); // 12/6/28
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"M/d"];
NSLog(@"%@", [formatter stringFromDate:[NSDate date]]); // 6/28は出力されるがフォーマットは他言語にローカライズされない
23
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
22