Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing NSDates

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?

like image 765
Garry Pettet Avatar asked Oct 15 '22 07:10

Garry Pettet


1 Answers

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.

like image 141
Alfons Avatar answered Oct 27 '22 07:10

Alfons