Frage

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.

War es hilfreich?

Lösung

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
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top