iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – datebyAddingTimeInterval不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在某些情况下,我需要在1天内增加一个NSDate.对于它,我使用dateByAddingTimeInterval,但它不起作用.

这是代码

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];
NSString *startDate = [NSString StringWithFormat:@"%@/2012 %@",dayString,begin];
NSString *endStringForDate = [NSString StringWithFormat:@"%@/2012 %@",end];

NSLog(@"Csantos: event starts: %@,event ends: %@",startDate,endStringForDatE);

NSDate *beginDate = [dateFormat dateFromString:startDate];
NSDate *endDate = [dateFormat dateFromString:endStringForDate];

NSComparisonResult result = [beginDate compare:endDate];


if(result == NSorderedDescending){
    NSTimeInterval dayinseconds = 24 * 60 * 60;

    [enddate DATEByAddingTimeInterval:dayinseconds];
    NSLog(@"Csantos: event ends: %@",endDatE);
}

结果:

2012-01-24 12:09:47.837 app[3689:207] Csantos: event starts: 19/02/2012 23:00,event ends: 19/02/2012 03:00
2012-01-24 12:09:47.837 app[3689:207] Csantos: event ends: 19/02/2012 03:00

我已经尝试过addTimeInterval(不推荐使用,我知道),但它也没有用.怎么了?

问候!

@H_607_21@解决方法
[enddate DATEByAddingTimeInterval:dayinseconds];返回一个值,该值是函数生成的新日期.日期是不可变对象(如字符串),因此您无法修改它们 – 您只能通过应用函数获得新日期.

如果你写这个,相反,它会工作:

endDate = [enddate DATEByAddingTimeInterval:dayinseconds];
@H_301_39@

大佬总结

以上是大佬教程为你收集整理的ios – datebyAddingTimeInterval不起作用全部内容,希望文章能够帮你解决ios – datebyAddingTimeInterval不起作用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。