views:

184

answers:

2
+1  Q: 

Comparing NSDates

Hi,

I have an NSDate object called 'dueDate'. I'm trying to work out how to display if the due date was yesterday or is tomorrow. How would I go about this?

+3  A: 

To see if a date is "tomorrow", do something like this.

    NSCalendar *calendar = [NSCalendar currentCalendar];    
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    // set tomorrow (0: today, -1: yesterday)
    [comps setDay:1];
    NSDate *dateTomorrow = [calendar dateByAddingComponents:comps 
                                                     toDate:currentDate  
                                                    options:0];
    [comps release];

The rest should be fairly obvious.

HTH.

Alfons
A: 
NSDate *today = [NSDate date];

NSTimeInterval dateTime;


if ([visitDate isEqualToDate:today])   //visitDate is a NSDate

{

NSLog (@"Dates are equal");

}

dateTime = ([visitDate timeIntervalSinceDate:today] / 86400);  

if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val

{

NSLog (@"Past Date");

}

else 

{   
NSLog (@"Future Date");

}
mihirpmehta