我的Json是
{
"MinPerAppointment": "30",
"OpeningTime": "12:05"
}
现在,我想添加30到12:05,应该是12:35,我无法获得如何格式化,所以,我尝试使用NSdate
NSString *str_MinPerAppointment = timeDetailData.minPerAppointment;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
NSDate *myDate = [dateFormatter dateFromString:str_MinPerAppointment];
NSString *dateInString = [dateFormatter stringFromDate:myDate];
NSLog(@"New AppointMentValue :%@",dateInString);
我将新的AppointMentValue改为 nil 。
答案 0 :(得分:2)
我认为您不需要使用NSDate
格式。只需将HH:MM
转换为分钟(或秒,如果你处理它们),做数学然后转换回来。
类似的东西:
NSString *time = @"12:30";
int addMinutes = 30;
int hh, mm;
if (sscanf([time UTF8String], "%d:%d", &hh, &mm) == 2) {
int minutes = (hh * 60) + mm;
minutes += addMinutes;
hh = minutes / 60;
mm = minutes - (hh * 60);
hh %= 24; // day roll-over
NSString *newTime = [NSString stringWithFormat:@"%02:%02d", hh, mm];
} else {
NSLog(@"Invalid time value: %@", time);
}
答案 1 :(得分:1)
如果你想使用NSDateFormatter/NSDate
试试这个 -
NSString *str_MinPerAppointment = @"30";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
NSDate *myDate = [dateFormatter dateFromString:@"12:05"] ;
NSString *dateInString = [dateFormatter stringFromDate:[myDate dateByAddingTimeInterval:60*[str_MinPerAppointment integerValue]]];
NSLog(@"New AppointMentValue :%@",dateInString);