NSDate Comparison - Which date is older?

William Welbes on January 10, 2011


I had to lookup how to do date comparisons in Objective-C this morning to be able to determine if one date is older than another:

NSDate * dateOne = [NSDate date];
NSDate * dateTwo = [NSDate date];

if([dateOne compare:dateTwo] == NSOrderedAscending) {
    // dateOne is before dateTwo
}

The above check will determine if dateOne is before dateTwo. The comparison will return one of the following codes which could potentially be used in a switch case if needed:

NSOrderedAscending //dateOne before dateTwo
NSOrderedSame  //dateOne and dateTwo are the same
NSOrderedDescending  //dateOne is after dateTwo

comments powered by Disqus