Привет, у меня есть строка 2011-05-13T00: 00: 00, и я хочу преобразовать эту строку в формате 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
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]; }
используйте этот код
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat: @ "гггг-мм-дд чч: мм: сс а"];
NSDate * myDate = [df dateFromString: myDateAsAStringValue];