質問

私はちょうど何かが欠けています確信しています。しかし、私はNSDateFormatterShortStyleのスタイルでNSDateを表示するときに4桁の年表示する方法を見つけようとし日間のためにこれをGoogleで検索しています。あなたが解決策を持っているなら、私に知らせてください。ここで私は今使用していたコードです。

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    // add label for birth
    UILabel *birthday = [[UILabel alloc] initWithFrame:CGRectMake(125, 10, 100, 25)];
    birthday.textAlignment = UITextAlignmentRight;
    birthday.tag = kLabelTag;
    birthday.font = [UIFont boldSystemFontOfSize:14];
    birthday.textColor = [UIColor grayColor];
    birthday.text = [dateFormatter stringFromDate:p.Birth];
役に立ちましたか?

解決

あなたは4桁の年が必要な場合は、

、あなたがしたい正確なフォーマットを使用してdateformat:を設定します。欠点は、あなたが自動ロケール形式を失うです。

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];

...

birthday.text = [dateFormatter stringFromDate:p.Birth];
あなたはローカライズが必要な場合は、

あなたは短いスタイルの日付形式を変更してみてください。

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

if ([[dateFormatter dateFormat] rangeOfString:@"yyyy"].location == NSNotFound) { 
    NSString *fourDigitYearFormat = [[dateFormatter dateFormat] stringByReplacingOccurrencesOfString:@"yy" withString:@"yyyy"]; 
    [dateFormatter setDateFormat:fourDigitYearFormat];             
}

の最大マクラウドからのバグ修正と更新

他のヒント

ただ、私は「やったように私は、ローカライズされた日付形式で2桁の年でそんなことを必要とcosを誰かがここでつまずくと、これは本当に古い問題である場合には、

、ここに私の解決策はあります:

NSLocale*           curLocale = [NSLocale currentLocale];
NSString*           dateFmt   = [NSDateFormatter dateFormatFromTemplate: @"ddMMyyyy"
                                                                options: 0
                                                                 locale: curLocale];
NSDateFormatter*    formatter = [[[NSDateFormatter alloc] init] autorelease];
                    [formatter setDateFormat:dateFmt];
NSString*           retText = [formatter stringFromDate:refDate];

return retText;

たぶん誰かがいつかそれを使用することができます...

この古いスレッドですが、私はデイブが望んでいたではなく、与えられた答えのどれが正しいなかったので、ここでは解決された可能性が何をしようとしていたので(今までしたい場合は誰でも同じことです):

NSString *FormatString = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy HH:mm" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:FormatString];
//Parse date here with the formater (like [formatter stringFromDate:date])
[formatter release];
  

kCFDateFormatterShortStyle   だけで、例えば、「11/23/37」または「15:30」のように、通常の数値短いスタイルを指定します。

     

のMac OS X 10.3以降で利用可能。

     

CFDateFormatter.hで宣言ます。

NSDateFormatterShortStyleはあなたに4桁の年を与えるためには表示されません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top