具有月份名称的日期Objective-C

问题描述 投票:8回答:3

我有一个有价值的NSDate对象例如:“2011-08-26 14:14:51”,我想显示“2011年8月8日”如何实现它?谢谢。

objective-c nsdate nsdateformatter
3个回答
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];

12
投票
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"dd LLLL yyyy"];
NSString *formattedDate = [df stringFromDate:[NSDate date]];

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);
© www.soinside.com 2019 - 2024. All rights reserved.