开发者

Comparing NSDates

开发者 https://www.devze.com 2022-12-25 10:31 出处:网络
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 开发者_C百科go about this?To see if a date is \"tomorrow\",

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 开发者_C百科go about this?


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.


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号