Question

I understand that NSDates could easily be compared using a function such as if ([date1 compare:date2] == NSOrderedDescending). However, I am interested in comparing two times only that were formatted with a NSDateFormatter and appears as HH:mm. For example, if I wanted to compare 07:20 with 08:10 and see which of the time is later, how would I be able to do this?

Edit: I understand a hacky way of doing it would be looking at each of the HH:mm digits and see which one is greater, but was wondering if there may be a time comparison function built into NSDate?

Thanks!

Was it helpful?

Solution

If you have two times using 24-hour format, then simply compare the two strings.

NSString *time1String = ... // some time in HH:mm
NSString *time2String = ... // some time in HH:mm

if ([time1String compare:time2String] == NSOrderedDescending) {
} else {
}

The string comparison will work as long as the hour and minutes are both two digits and the hour is in 24 hour format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top