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 开发者_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");
}
精彩评论