比较NSDates

Gar*_*tet 5 iphone compare objective-c nsdate

我有一个名为'dueDate'的NSDate对象.我正在尝试如何显示截止日期是昨天还是明天.我该怎么做?

小智 4

要查看某个日期是否是“明天”,请执行以下操作。

    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];
Run Code Online (Sandbox Code Playgroud)

其余的应该是相当明显的。

HTH。