-2

I want to compare two dates and do something when one is less or equal than the other, I tried to do it this way:

if ([self.system.systemInbetriebnahme compare:date] == NSOrderedDescending)
        return nil;

But this removes the case when they are both equal is there a way to compare dates with >= ?

Thank you

More details: I have a date taken from a double dimension array, and i have a date stored in core data(through date picker), and i go through a loop to compare the two dates with the rule that: Date in array must be greater or equal than input date stored in database.

Latest Edit:

  • I import a csv file which after parsing and formatting gives me two rows(one for number value and one for dates). Here I am interested in dates.

  • I store all these in an array and save after checking consistency of the data in this function:

    for (NSArray * row in array) {
    
    
     // Handle import if there are at least two rows
    
    
    if (row.count > 1) {
    
                dispatch_sync(dispatch_get_main_queue(), ^{
                    // Try to import value,date formatted row
                    double value = [row[0] doubleValue];
                    NSDate * date = [dateFormatter dateFromString:row[1]];
                   //else {
    
                        // Try to import date,value row
    
                        if (date && value != 0.0) {
                            [self addReading:value forDate:date];
                        }
    
  • In here i call the function addreading to check for errors for some rules and if everything is okey then i save the managedObject Context.

     if ([installationdatum compare:date] != NSOrderedAscending)
                   { 
                   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                     message:@"installation date is bigger than data"
                                                                      delegate: nil
                                                             cancelButtonTitle:@"OK"
                                                             otherButtonTitles:nil] 
    
                        [alert show];
                       return nil;
    
    
    
                   } else
                   {  //Do nothing
    
                   }
    
  • One rule for me is to get the first row element in the array and compare its date with an input stored already in the database which is the self.system.systemInbetriebnahme

  • After all this I list all the resulting array in a tableview, though value matching NSOrderedSame is never shown when it is equal to self.system.systemInbetriebnahme

Souhail Marghabi
  • 127
  • 1
  • 2
  • 11

8 Answers8

12

Simple...

if ([date1 compare:date2] == NSOrderedDescending) {
    NSLog(@"date1 is later than date2");        

} else if ([date1 compare:date2] == NSOrderedAscending) {
    NSLog(@"date1 is earlier than date2");

} else {
    NSLog(@"dates are the same");

}
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • 1
    Copy&pasted from [the answer of a different person](http://stackoverflow.com/a/5965106/400056) without attribution. Please do not do that. – DarkDust Feb 18 '15 at 15:27
  • @DarkDust: Yes it was copied to my code(project) before this question, Later I searched for the link, I didn't get so . I copied from my code n added here. – Kumar KL Feb 19 '15 at 04:30
11

Simply check for not ascending order.

if ([self.system.systemInbetriebnahme compare:date] != NSOrderedAscending)
        return nil;
Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
3
NSDate *toDay = [[NSDate alloc]init];

NSLog(@"%@",toDay);

 NSLog(@"%@",yourSecondDate);

if ([toDay timeIntervalSinceDate:yourSecondDate] < 30.0f) 
 {
    NSLog(@"Do Something");
 }
Bug
  • 2,576
  • 2
  • 21
  • 36
1

Well >= just means that they are !<, so you can check

if ([self.system.systemInbetriebnahme compare:date] != NSOrderedAscending)
Levi
  • 7,313
  • 2
  • 32
  • 44
1

You could either check if the result is equal to both NSOrderedDescending and NSOrderedSame:

NSComparisonResult comparisonResult = [self.system.systemInbetriebnahme compare:date]
if (comparisonResult == NSOrderedDescending  || comparisonResult == NSOrderedSame)
        return nil;

Or just not match the one option left:

NSComparisonResult comparisonResult = [self.system.systemInbetriebnahme compare:date]
if (comparisonResult != NSOrderedAscending)
        return nil;

In your case I would go the later option, since you only have 3 options check against the one you don't want is easiest.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
0

I think this will help you. do something when one is less or equal than the other as you looking different case.

switch ([self.system.systemInbetriebnahme compare: date]) {
case NSOrderedAscending:
    // system date earlier then date
    break;
case NSOrderedSame:
    //  Same
    break;
case NSOrderedDescending:
    // system date later then date
    break;
}
Buntylm
  • 7,345
  • 1
  • 31
  • 51
0

there is no direct comparision like >= but you can do it like this

NSComparisonResult compareStart = [date1 compare: selectedDate]; // date1 is 6/6/2011 00:00
NSComparisonResult compareEnd = [date2 compare: selectedDate];   // date2 is 9/6/2011 00:00

if ((compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame)
&& (compareEnd == NSOrderedDescending))
{
// date is in the right range
}

or you simply compare

if ((compareStart == NSOrderedAscending) )
{
// date is in the right range
}
Anurag Soni
  • 1,077
  • 10
  • 14
0

To com[are two NSDate, this :

[[date1 earlierDate:date2] isEqualToDate: date1]

returns YES if the date1 is earlier than date2.

EDIT

if you want date1 >= date2 (later or equals to) use this condition :

([[date1 laterDate:date2] isEqualToDate: date1] || [date2 isEqualToDate:date1])
zbMax
  • 2,756
  • 3
  • 21
  • 42
  • But with a minimum search in the Apple documentation, you would not have to post such a question... – zbMax Aug 27 '13 at 12:51
  • I am asking specificly about >= for NSDates. There was no mention in it in the app doc or results given by stackoverflow search, so I wanted to ask here. Thank you. – Souhail Marghabi Aug 27 '13 at 13:09