문제

I have two UIDatePickers(Time mode) in my project and I'd like to know how to check if the times in the two UIDatePicker are atleast, for example, 3 minutes apart. This is probably an easy question but I'm still learning, hope you guys understand.

Thanks.

도움이 되었습니까?

해결책

Get the two NSDate values from the pickers. Then do:

NSDate *date1 = // first date
NSDate *date2 = // second date
NSTimeInterval diff = fabs([date1 timeIntervalSinceDate:date2]);
if (diff > 3 * 60) {
    // the difference is more than 3 minutes
}

Another options would be:

NSDate *date1 = // first date
NSDate *date2 = // second date
unsigned int unitFlags = NSMinuteCalendarUnit;
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:date2  options:0];
int minutes = [comps minute];
if (minutes > 3) {
    // the difference is more than 3 minutes
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top