我有这个日期格式化程序:
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"HH:mm"];
Run Code Online (Sandbox Code Playgroud)
如果我用这个:
NSDate *myDate = [timeFormatter dateFromString:@"13:00"];
Run Code Online (Sandbox Code Playgroud)
它返回:
"1:00"
Run Code Online (Sandbox Code Playgroud)
这是因为模拟器已关闭24小时.但对于我的应用程序,我真的需要"13:00"而不是"1:00"
--- 编辑1 ---
添加了新代码:
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"HH:mm"];
NSDate *timeForFirstRow = [timeFormatter dateFromString:@"13:00"];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:timeForFirstRow];
NSInteger hour = [dateComponents hour]; //This will be 1 instead of 13
NSInteger minute = [dateComponents minute];
Run Code Online (Sandbox Code Playgroud)