gpt4 book ai didi

ios - 将字符串转换为时间格式将返回nil数据

转载 作者:行者123 更新时间:2023-12-01 18:43:57 25 4
gpt4 key购买 nike

我的杰森是

{
"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);

我得到New AppointMentValue作为 nil

最佳答案

我认为您不需要使用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);
}

关于ios - 将字符串转换为时间格式将返回nil数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38346155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com