Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a date is of the same calendar day

Could someone please help me figure out how to check if some date is of the same day as today. I guess it would require creating a calender day at 0 hour of the same day in the same timezone and checking against that, but so far my attempts have confused me more then anything.

like image 946
dizy Avatar asked Oct 29 '25 05:10

dizy


1 Answers

NSCalendar lets you deal with human days. So you could implement a category something like this:

@implementation NSDate (IsItToday)
- (BOOL)isToday {
    NSUInteger desiredComponents = NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
    NSDateComponents *myCalendarDate = [[NSCalendar currentCalendar] components:desiredComponents fromDate:self];
    NSDateComponents *today = [[NSCalendar currentCalendar] components:desiredComponents fromDate:[NSDate date]];
    return [myCalendarDate isEqual:today];
}
@end
like image 96
Chuck Avatar answered Oct 30 '25 22:10

Chuck