date格式问题 – 将string转换为date格式为MMM d,yyyy格式

嗨,我有一个string2011-05-13T00:00:00,我想转换这个string在MMM D,YYYY格式。 任何帮助?

阅读: http : //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

我不在我的Mac,但这应该给你一个开始:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"]; NSDate *originalDate = [dateFormatter dateFromString:originalDateString]; [dateFormatter setDateFormat:@"MM' 'DD','yyyy"]; NSString *formattedDateString = [dateFormatter stringFromDate:originalDate]; [dateFormatter release]; 

你应该看看NSDateFormatter的参考文档,它正是你想要的: http : //developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

使用这个代码

NSDateFormatter * df = [[NSDateFormatter alloc] init];

[df setDateFormat:@“yyyy-MM-dd hh:mm:ss a”];

NSDate * myDate = [df dateFromString:myDateAsAStringValue];

 On Class where Date format is required (Implementation class) -(void)DateFormatter:stringDateToBeFormatted { NSDate *dateFormatter=[NSDate convertStringToDate:stringDateToBeFormatted]; NSString *requiredDateFormat= [NSDate dateStringWithMediumFormat: dateFromatter]; self.stringDateFormatLable.Text=[NSString stringWithFormat:@"date required %@",requiredDateFormat] } On NSDate Class implementation class +(NSString *)dateStringWithMediumFormat:(NSDate *)date { NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; or [dateFormatter setDateFormat:@"MMM-dd-yyyy"]; return [dateFormatter stringFromDate:date]; } +(NSDate *)convertStringToDate:(NSString *)dateString { NSDateFormatter *dateFormatter= [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'hh':'mm':'ss"]; return [dateFormatter dateFromString:dateString]; }