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

Date formatter returning null in some cases (after 12:59:59)

我收到了这样的 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" 返回 null。

所有高于 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.

特别是 "it should not match a 24-hour-cycle format" 对您很重要,因为您实际上想要明确匹配 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