输入:Fri, 26 Apr 2013 21:56:31 EDT
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:@"EE, dd MMM YYYY HH:mm:ss zzz"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *startDateStr2 = [df1 stringFromDate:[NSDate date]];
NSLog(@"currentDate: %@", startDateStr2);但是我得到了输出: Sat, 05 Jan 2013 07:26:31 GMT+05:30
发布于 2013-04-29 04:56:44
使用下面的方法,用于更改NSDate的格式。
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);
return convertedString;
}用它的三个参数调用上述方法。
1. stringDate :以字符串类型传递您的日期。
2. dateFormat :在传递的字符串日期中使用的 set格式。例如,如果字符串日期为"14-03-2013 11:22 am",则将格式设置为@"dd-MM-yyyy hh:mm a"。
3. getwithFormat :您想要的设置格式,就好像您想要日期14/03/2013,然后只设置格式@"dd/MM/yyyy"。
如何调用此方法请参见下面的示例:
NSString *strSDate = [self changeDateFormat:@"14-03-2013 11:22 am" dateFormat:@"dd-MM-yyyy hh:mm a" getwithFormat:@"dd/MM/yyyy"];
NSLog(@"\n\n Now Date => %@",strSDate);输出为: Now Date => 14/03/2013
希望对你有帮助..。
https://stackoverflow.com/questions/16271399
复制相似问题