NSTimeInterval distanceBetweenDates = [StartDate timeIntervalSinceDate:EndDate];
if (distanceBetweenDates > 0) {
NSLog("Greather than ")
} else if (distanceBetweenDates < 0) {
NSLog("less than");
} else {
NSLog("Equal");
}
Question
I have three dates 'startDate', stopDate
and currentDate
. I need compare 'startDate' <= 'currentDate' and 'stopDate' >= 'currentDate'. My code.
unsigned int flagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* fromComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:startDate];
NSDate* fromDateOnly = [[NSCalendar currentCalendar] dateFromComponents:fromComponents];
NSDateComponents* toComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:stopDate];
NSDate* toDateOnly = [[NSCalendar currentCalendar] dateFromComponents:toComponents];
NSDate * currentDate = [NSDate date];
NSDateComponents* currComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate: currentDate];
NSDate* currDateOnly = [[NSCalendar currentCalendar] dateFromComponents:currComponents];
NSTimeInterval fromTime = [fromDateOnly timeIntervalSinceReferenceDate];
NSTimeInterval toTime = [toDateOnly timeIntervalSinceReferenceDate];
NSTimeInterval currTime = [currDateOnly timeIntervalSinceReferenceDate];
NSLog(@"%@ %@",currentDate, currDateOnly);
NSLog(@"%@ %@",startDate, fromDateOnly);
NSLog(@"%@ %@",stopDate, toDateOnly);
That's what brings nslog.
2014-04-10 23:59:53.280 Calendar[2148:90b] 2014-04-10 19:59:53 +0000 2014-04-09 20:00:00 +0000 2014-04-10 23:59:53.282 Calendar[2148:90b] 2014-04-10 20:14:12 +0000 2014-04-10 20:00:00 +0000 2014-04-10 23:59:53.282 Calendar[2148:90b] 2015-04-09 20:14:12 +0000 2015-04-09 20:00:00 +0000
How do I compare dates? I need to compare dates only without time. Why change the date if time is less than 20:00?
Solution
NSTimeInterval distanceBetweenDates = [StartDate timeIntervalSinceDate:EndDate];
if (distanceBetweenDates > 0) {
NSLog("Greather than ")
} else if (distanceBetweenDates < 0) {
NSLog("less than");
} else {
NSLog("Equal");
}
OTHER TIPS
Alexey, I suggest you incorporate this wonderful NSDate extension in your project: https://github.com/erica/NSDate-Extensions
You can then use
- (NSInteger)distanceInDaysToDate:(NSDate *)anotherDate
And check if it's < 0, 0 or > 0
If you wanted to compare only the dates, do as follows:
NSInteger flagsDate = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDateComponents* fromComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:startDate];
NSDateComponents* toComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:stopDate];
NSDateComponents* currComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:cu];
NSComparisonResult firstComparison = [[fromComponents date] compare: [currComponents date]];
NSComparisonResult secondComparison = [[toComponents date] compare: [currComponents date]];
then to check your result
if (firstComparison == NSOrderedDescending || firstComparison == NSOrderedSame) {}
if (secondComparison == NSOrderedSame || secondComparison == NSOrderedAscending) {}