比较NSDates

时间:2010-04-05 07:44:10

标签: iphone objective-c compare nsdate

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

2 个答案:

答案 0 :(得分: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];

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

HTH。

答案 1 :(得分:0)

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

}