HI, i have a string 2011-05-13T00:00:00 and i want to convert this string in MMM d, yyyy format. any Help?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Read this: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
I'm not at my Mac, but this should give you a head-start:
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];
回答2:
You should look at the reference documentation for NSDateFormatter
, it do exactly what you want: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
回答3:
use this code
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString: myDateAsAStringValue];
回答4:
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];
}