Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Compare two dates

I have a NSDate that I must compare with other two NSDate and I try with NSOrderAscending and NSOrderDescending but if my date is equal at other two dates?

Example: if I have a myDate = 24/05/2011 and other two that are one = 24/05/2011 and two 24/05/2011 what can I use?

like image 593
cyclingIsBetter Avatar asked May 24 '11 14:05

cyclingIsBetter


People also ask

How do I compare two date dates?

Use the datetime Module and the < / > Operator to Compare Two Dates in Python. datetime and simple comparison operators < or > can be used to compare two dates.

How do I compare two dates in Swift?

Here we will create two different date objects using DateFormatter. Then use compare function of Date class to check whether our two different date objects are same,one date is greater than other date or one date is smaller than other date in swift.

How do you compare two dates without the time portion?

In Java 8 you can easily compare two dates without having time because Java 8 Date API provides LocalDate final class that gives you only date part. So using LocalDate object you can use methods, such as, isBefore() , isAfter() to compare your date part only.


2 Answers

According to Apple documentation of NSDate compare:

Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.

- (NSComparisonResult)compare:(NSDate *)anotherDate

Parameters anotherDate

The date with which to compare the receiver. This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.

Return Value

If:

The receiver and anotherDate are exactly equal to each other, NSOrderedSame

The receiver is later in time than anotherDate, NSOrderedDescending

The receiver is earlier in time than anotherDate, NSOrderedAscending

In other words:

if ([date1 compare:date2] == NSOrderedSame) ... 

Note that it might be easier in your particular case to read and write this :

if ([date2 isEqualToDate:date2]) ... 

See Apple Documentation about this one.

like image 167
Vincent Guerci Avatar answered Oct 24 '22 17:10

Vincent Guerci


After searching, I've got to conclusion that the best way of doing it is like this:

- (BOOL)isEndDateIsSmallerThanCurrent:(NSDate *)checkEndDate {     NSDate* enddate = checkEndDate;     NSDate* currentdate = [NSDate date];     NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];     double secondsInMinute = 60;     NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;      if (secondsBetweenDates == 0)         return YES;     else if (secondsBetweenDates < 0)         return YES;     else         return NO; } 

You can change it to difference between hours also.


If you want to compare date with format of dd/MM/yyyy only, you need to add below lines between NSDate* currentdate = [NSDate date]; && NSTimeInterval distance

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd/MM/yyyy"]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]                           autorelease]];  NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];  currentdate = [dateFormatter dateFromString:stringDate]; 
like image 37
ytpm Avatar answered Oct 24 '22 17:10

ytpm