如何将日期格式从mm / dd / yyyy hh:mm:ss:sss转换为EEE dd MMM,yyyy,hh:mm:ss in ios

时间:2014-11-28 05:15:35

标签: ios iphone datetime nsdate nsdateformatter

我从webservice获取格式为mm/dd/yyyy hh:mm:ss:sss的日期字符串(11/28/2014 10:21:33:940)(而不是我当地的机器时间),我必须将其转换为此格式Fri 28 NOV 2014 10:21:33:940并在iOS的用户界面中显示。任何人都可以帮助我获得这种格式,提前致谢

1 个答案:

答案 0 :(得分:1)

尝试这个,它对我来说很好。

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; // 10.4+ style
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss:sss"];
 NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];

 NSDate *tempDate = [dateFormatter dateFromString:dateStr];

[dateFormatter setDateFormat:@"EEE dd MMM, yyyy, HH:mm:ss:sss"];
 NSString *newFormatedDateStr = [dateFormatter stringFromDate:tempDate];
 NSLog(@"newFormatedDateStr is :%@",newFormatedDateStr);

//It's give -> newFormatedDateStr is :Fri 28 Nov, 2014, 11:47:32:032

<强>更新

NSString *myDateString1=@"11/28/2014 11:46:33.310";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss.SSS";//"SSS" is for milliseconds.
NSDate* DATE = [dateFormatter dateFromString:myDateString1];

NSLog(@"%@", DATE);

了解更多详情:http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

快乐编码.. :)