user1051935
user1051935

Reputation: 589

Objective C , comparing two dates

I am trying to compare two dates but I can t see how it works at all ...

this is the code that I am using :

   NSCalendar* myCalendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 

                                                 fromDate:[NSDate date]];
    [components setYear:2012];
    [components setMonth:03];
    [components setDay:25];
    NSDate *startDate = [myCalendar dateFromComponents:components];


    NSDateComponents* components2 = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 

                                                 fromDate:[NSDate date]];
    [components2 setYear:2012];
    [components2 setMonth:04];
    [components2 setDay:25];
    NSDate *endDate = [myCalendar dateFromComponents:components];

    NSComparisonResult result1 = [now compare:startDate];
    NSComparisonResult result2 = [now compare:endDate];

    if (result2 &&

        result1
        ) {
        int x =1;
    }

the thing is even if the two dates are in the future , i still can get into the if condition ... any reason ?

Upvotes: 0

Views: 860

Answers (4)

Akhtar
Akhtar

Reputation: 3202

By using this function you can compare two dates.This is tested function and working fine on my end.

-(BOOL)dateComparision:(NSDate*)date1 andDate2:(NSDate*)date2{

BOOL isTokonValid;

if ([date1 compare:date2] == NSOrderedDescending) {
    NSLog(@"date1 is later than date2");
    isTokonValid = YES;
} else if ([date1 compare:date2] == NSOrderedAscending) {
    NSLog(@"date1 is earlier than date2");
    isTokonValid = NO;
} else {
    isTokonValid = NO;
    NSLog(@"dates are the same");
}

return isTokonValid;

}

Upvotes: 0

Parag Bafna
Parag Bafna

Reputation: 22930

try this.

NSCalendar* myCalendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 

                                                 fromDate:[NSDate date]];
    [components setYear:2012];
    [components setMonth:03];
    [components setDay:25];
    NSDate *startDate = [myCalendar dateFromComponents:components];


    NSDateComponents* components2 = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 

                                                 fromDate:[NSDate date]];
    [components2 setYear:2012];
    [components2 setMonth:04];
    [components2 setDay:25];
    NSDate *endDate = [myCalendar dateFromComponents:components];

    NSComparisonResult result1 = [now compare:startDate];
    NSComparisonResult result2 = [now compare:endDate];

    if (result2 == NSOrderedSame && result1 == NSOrderedSame ) {
        int x =1;
    }  

NSComparisonResult

These constants are used to indicate how items in a request are ordered.

enum {
    NSOrderedAscending = -1,
    NSOrderedSame=0,
    NSOrderedDescending=1
};
typedef NSInteger NSComparisonResult;

Upvotes: 0

kevboh
kevboh

Reputation: 5245

An NSComparisonResult is not a boolean. See the documentation. Depending on how you want to if to be entered, you should check for NSOrderedAscending (now is earlier), NSOrderedSame (same dates), or NSOrderedDescending (now is later).

Upvotes: 1

daxnitro
daxnitro

Reputation: 920

The values of result1 and result2 are one of the following.

enum {
    NSOrderedAscending = -1,
    NSOrderedSame,
    NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

The only time (result2 && result1) wouldn't return true is when one of them evaluates to false or 0. This only happens if one of the dates is exactly the same as now.

if (result1 == NSOrderedAscending) {
    // result1 is in the future
}

if (result1 == NSOrderedDescending) {
    // result1 is in the past
}

Upvotes: 2

Related Questions