IOS日期格式问题

时间:2016-09-06 11:13:41

标签: ios objective-c nsdateformatter

我有像这种格式的日期

" lastUpdatedTime":" 2016-09-03 02:01 AM",

我必须格式化为03 Sep 2016 02:01 am

我尝试了dateFormatter

它不起作用

 NSString *lastUpdatedTime = [JSONResultDict valueForKey:@"lastUpdatedTime"];

 NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
 NSDate *yourDate = [dateFormatter dateFromString:lastUpdatedTime];

但你的日期是零

4 个答案:

答案 0 :(得分:1)

试试这个:

data.frame

答案 1 :(得分:0)

您需要使用yyyy-MM-dd hh:mm a格式进行12小时时间格式化,您需要使用hh代替HH

NSString *lastUpdatedTime = [JSONResultDict valueForKey:@"lastUpdatedTime"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:lastUpdatedTime];
dateFormatter.dateFormat = @"dd MMM yyyy hh:mm a";
NSString *formttedDate = [dateFormatter stringFromDate: yourDate];
NSLog(@"%@",formttedDate);

<强>输出

03 Sep 2016 02:01 AM

答案 2 :(得分:0)

试试这段代码:

NSString *lastUpdatedTime = [JSONResultDict valueForKey:@"lastUpdatedTime"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a";
NSDate *yourDate = [dateFormatter dateFromString:lastUpdatedTime];
dateFormatter.dateFormat = @"dd MMM yyyy HH:mm a";
NSLog(@"Date: %@",[dateFormatter stringFromDate:yourDate]);

答案 3 :(得分:0)

您应该使用稍微不同的日期格式并将其更改为您想要的格式,如下所示:

NSString *lastUpdatedTime = [JSONResultDict valueForKey:@"lastUpdatedTime"];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd hh:mm a";
NSDate * yourDate = [formatter dateFromString:lastUpdatedTime];
formatter.dateFormat = @"dd-MM-yyyy hh:mm a";
NSLog(@"my date is: %@",[formatter stringFromDate:yourDate]);