我有一个名为“dueDate”的NSDate对象。我想知道如何显示该日期是昨天还是明天。请问应该如何实现?
我有一个名为“dueDate”的NSDate对象。我想知道如何显示该日期是昨天还是明天。请问应该如何实现?
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];
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");
}