我收到这样的 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/