ios - 日期格式化程序在某些情况下返回 null(在 12 :59:59) 之后

标签 ios objective-c json nsdateformatter

我收到这样的 JSON 响应。

{
        0 = 50;
        1 = 1;
        2 = 4;
        3 = "08:51:00";
        4 = "20:51:00";
        Id = 50;
        day = 4;
        endTime = "20:51:00";
        openTime = "08:51:00";
        venId = 1;

我正在使用日期格式化程序将字符串转换为日期,然后使用此代码将其转换为适当的字符串格式。

NSDateFormatter * DateFormat = [[NSDateFormatter alloc] init];
[DateFormat setDateFormat:@"hh:mm:ss"];

NSDateFormatter * recevingDateFormatter = [[NSDateFormatter alloc] init];
[recevingDateFormatter setDateFormat:@"hh:mm aa"];

NSDate * openTime = [DateFormat dateFromString:[[_arrHoursOfOpr objectAtIndex:indexPath.row] objectForKey:@"openTime"]];
NSDate * endTime = [DateFormat dateFromString:[[_arrHoursOfOpr objectAtIndex:indexPath.row] objectForKey:@"endTime"]];
NSLog(@"the start time and end time is %@, %@", openTime, endTime);
objCell.txtDay.text =  [_arr_weekdays objectAtIndex:[[[_arrHoursOfOpr objectAtIndex:indexPath.row]objectForKey:@"day"] integerValue]];
objCell.txtStartTime.text = [recevingDateFormatter stringFromDate:openTime];
objCell.txtEndTime.text =[recevingDateFormatter stringFromDate:endTime];

但不知何故“openTime”被转换而“endTime”返回空值。

所有超过 12:59:59 的字符串都会发生这种情况,即从时间 13:00:00 开始。

我如何解决该问题并在 12:59:59 之后提供支持?

最佳答案

hh表示小时,范围是[1-12]。

Hour [1-12]. When used in skeleton data or in a skeleton passed in an API for flexible date pattern generation, it should match the 12-hour-cycle format preferred by the locale (h or K); it should not match a 24-hour-cycle format (H or k). Use hh for zero padding.

特别是“它不应该匹配 24 小时制”对您很重要,因为您实际上想要明确匹配 24 小时制。

您需要使用 HH 来支持 13 小时以上:

Hour [0-23]. When used in skeleton data or in a skeleton passed in an API for flexible date pattern generation, it should match the 24-hour-cycle format preferred by the locale (H or k); it should not match a 12-hour-cycle format (h or K). Use HH for zero padding.

参见 unicode docs

关于ios - 日期格式化程序在某些情况下返回 null(在 12 :59:59) 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31401658/

相关文章:

ios - 在 IOS6 SDK 上编译最新版本的 admob 时不断收到错误消息

objective-c - 如何使用 subview 对缩放 UIView 进行动画处理?

json - 机器学习友好的数据组织

javascript - 在 Angular js 中按类名过滤匹配的元素。

javascript - 为什么即使控制台记录了我的 Ajax JSON 数据也无法正确返回?

ios - 在应用程序运行后,是否可以在 ui 测试中以编程方式将应用程序移动到 Split View/ slider 上方的 View ?

ios - 如何使用 Kingfisher 仅在磁盘中缓存图像?

objective-c - cocoa 的PDFKit : disable "print to pdf" options

iphone - 如何在for循环中释放一个对象?

ios - Objective-C 中的 NSInvalidArgumentException