我是目标 c 的新手。我希望执行以下操作:将 24 小时格式转换为 12 小时,然后将 +2 添加到小时并显示为:下午 4:00 我得到 12 小时格式,但在添加 +2 后,时间始终显示为“am”,即即使是下午 4 点,它也会显示为凌晨 4 点。下面是我的代码:
NSDate *now = [NSDate date];
NSDateFormatter *timeFormatter=[[NSDateFormatter alloc]init];
timeFormatter.dateFormat=@"hh:00 a";
NSString *currentHour=[timeFormatter stringFromDate:now ];
lblcurrentHour.text=[NSString stringWithFormat:@"%@",currentHour];
NSLog(@"%@",currentHour);
int hour=[[timeFormatter stringFromDate:now]intValue];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = @"HH:mm";
NSDate *date1 = [dateFormatter1 dateFromString:[NSString stringWithFormat:@"%02d:00",hour+=3]];
dateFormatter1.dateFormat = @"hh:mm a";
lblnextHour.text = [dateFormatter1 stringFromDate:date1]; // prints 4:00 am not pm
我该如何解决这个问题?我哪里错了?