2011-09-08 9 views
8

NSDateオブジェクトの値が "2011-08-26 14:14:51"の場合、 "2011年8月8日"の表示方法を教えてください。ありがとう。月の名前Objective-C

答えて

16
NSDate *date = [NSDate date]; //I'm using this just to show the this is how you convert a date 

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateStyle:NSDateFormatterLongStyle]; // day, Full month and year 
[df setTimeStyle:NSDateFormatterNoStyle]; // nothing 

NSString *dateString = [df stringFromDate:date]; [df release]; 
+0

このメソッドは動作しません。[df setDateFormat:NSDateFormatterLongStyle]; - エラーとここ - '[df setTimeFormate: NSDateFormatterNoStyle]; ' – LightNight

+0

@mazder - apologies - 今回は編集しました。もう一回やってみよう。 – Abizern

+0

ありがとう、今は大丈夫です。しかし、私は文字列から日付を取得しようとしているときに '2011年1月26日'と表示されますなぜですか? – LightNight

6
NSString *dateStr = @"2011-08-26 14:14:51"; 

//if you have date then, 
NSString *dateStr = [NSString stringWithFormat:@"%@",yourDate]; 



NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; 
NSDate *date = [dateFormat dateFromString:dateStr]; 
[dateFormat setDateFormat:@"MMMM dd, YYYY"]; 
NSString* temp = [dateFormat stringFromDate:date]; 

[dateFormat release]; 



NSLog(@"%@",temp); 
+0

あなたの日付書式文字列が間違っています。 @ "YYYY-MM-dd HH:mm:ss"にする必要があります。 Dは月の日を指します – Abizern

+0

これは大丈夫です! – mayuur

12
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; 
[df setDateFormat:@"dd LLLL yyyy"]; 
NSString *formattedDate = [df stringFromDate:[NSDate date]]; 
関連する問題