简体   繁体   English

在 Objective-C 中添加两个 NSDate

[英]Addition of two NSDate in Objective-C

I have two NSDates with this format : "Hms" and I want to do an addition of this two dates to compare the result with my current time on my phone.我有两个具有这种格式的 NSDates:“Hms”,我想将这两个日期相加,以将结果与我手机上的当前时间进行比较。 I have all the information as you can see bellow :我有你可以看到的所有信息:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"H:m:s"];
    NSDate *start = [dateFormat dateFromString:NextTVS.startTime];
    NSDate *duration = [dateFormat dateFromString:NextTVS.duration];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *date = [NSDate date];
    [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];

However I don't know how to add start and duration.但是我不知道如何添加开始和持续时间。

Thanks for your help谢谢你的帮助

Use the dateByAddingComponents method of the NSCalendar class like so:像这样使用NSCalendar类的dateByAddingComponents方法:

NSDateComponents *comps = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
NSDate *newDate = [calendar dateByAddingComponents: comps toDate: date options:0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM