月份名称为Objective-C的日期
我有一个带有值的月份名称为Objective-C的日期,objective-c,nsdate,nsdateformatter,Objective C,Nsdate,Nsdateformatter,我有一个带有值的NSDate对象,例如:“2011-08-26 14:14:51”,我想显示“2011年8月8日”如何实现它?谢谢 这种方法行不通[df setDateFormat:NSDateFormatterLongStyle]-错误,此处-[df setTimeFormate:NSDateFormatterNoStyle]@mazder-抱歉-我这次编辑了它。再试一次。谢谢,现在可以了。。但当我试图从字符串中获取日期时,它会显示2011年1月26日why?因为Mayur Joshi给出的格
NSDate
对象,例如:“2011-08-26 14:14:51”,我想显示“2011年8月8日”如何实现它?谢谢 这种方法行不通<代码>[df setDateFormat:NSDateFormatterLongStyle]代码>-错误,此处-[df setTimeFormate:NSDateFormatterNoStyle]代码>@mazder-抱歉-我这次编辑了它。再试一次。谢谢,现在可以了。。但当我试图从字符串中获取日期时,它会显示2011年1月26日
why?因为Mayur Joshi给出的格式是错误的。它应该是@“YYYY-MM-dd HH:MM:ss”,因为使用D表示1..365年的天数,而不是使用dd的月份天数。您的日期格式字符串是错误的。它应该是@“YYYY-MM-dd HH:MM:ss”。D指一年中的天数D指一个月中的天数查看所有NSDATE格式设置程序。
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);
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];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"dd LLLL yyyy"];
NSString *formattedDate = [df stringFromDate:[NSDate date]];