iOS: Compare two less or equal NSDates [duplicate]

馋奶兔 提交于 2019-12-14 03:35:57

问题


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


回答1:


Simply check for not ascending order.

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



回答2:


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");

}



回答3:


NSDate *toDay = [[NSDate alloc]init];

NSLog(@"%@",toDay);

 NSLog(@"%@",yourSecondDate);

if ([toDay timeIntervalSinceDate:yourSecondDate] < 30.0f) 
 {
    NSLog(@"Do Something");
 }



回答4:


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

if ([self.system.systemInbetriebnahme compare:date] != NSOrderedAscending)



回答5:


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.




回答6:


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;
}



回答7:


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
}



回答8:


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])


来源:https://stackoverflow.com/questions/18465502/ios-compare-two-less-or-equal-nsdates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!