【发布时间】:2011-09-08 11:48:55
【问题描述】:
我有一个NSDate 对象,其值为例如:“2011-08-26 14:14:51”,我想显示“2011 年 8 月 8 日”如何实现?谢谢。
【问题讨论】:
-
在nsdateformatter.com查看所有NSDateFormatters。
标签: objective-c nsdate nsdateformatter
我有一个NSDate 对象,其值为例如:“2011-08-26 14:14:51”,我想显示“2011 年 8 月 8 日”如何实现?谢谢。
【问题讨论】:
标签: objective-c nsdate nsdateformatter
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];
【讨论】:
[df setDateFormat:NSDateFormatterLongStyle]; - 错误在这里 - [df setTimeFormate:NSDateFormatterNoStyle];
January 26, 2011 为什么?
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"dd LLLL yyyy"];
NSString *formattedDate = [df stringFromDate:[NSDate date]];
【讨论】:
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);
【讨论】: