"Check if NSDate is today"

Sat 13 February 2010

NSDate today = [NSDate date];

NSCalendar gregorian = [[NSCalendar alloc]

 initWithCalendarIdentifier:NSGregorianCalendar];

 

NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit |NSYearCalendarUnitfromDate:today];

NSInteger todayDay = [weekdayComponents day];

NSInteger todayMonth = [weekdayComponents month];

NSInteger todayYear = [weekdayComponents year];

 

BOOL res = NO;

 

//check if datedump is today

weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit)fromDate:dateDump];

NSInteger cday = [weekdayComponents day];

NSInteger month = [weekdayComponents month];

NSInteger year = [weekdayComponents year];

 

if (cday == todayDay &&

month == todayMonth &&

year == todayYear){

res = YES;

}