时间从12小时格式转换为24小时格式

时间:2015-09-29 11:35:49

标签: ios objective-c nsdateformatter

我编写了以下代码,将时间从12小时格式转换为24小时格式。它适用于模拟器,但在使用设备时它返回null。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat =@"hh:mm a";
NSString * endTimeString=[NSString stringWithFormat:@"%@",[[tempArray objectAtIndex:i] objectForKey:@"EndTime"]];
NSLog(@"Endtime from server responce:%@",endTimeString);
NSDate *date = [dateFormatter dateFromString:endTimeString];
dateFormatter.dateFormat = @"HH:mm" ;
NSString *pmamDateString = [dateFormatter stringFromDate:date];
NSLog(@"Converted EndTime:%@ ",pmamDateString);

2 个答案:

答案 0 :(得分:1)

尝试在NSDateFormatter上设置区域设置:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];

答案 1 :(得分:0)

朋友们,

       Issue is with the device settings ,to resolve this issue i changed my device time format as "automatic" ,now its working fine with those settings change.