Skip to content

Instantly share code, notes, and snippets.

@batosai
Created May 14, 2012 21:35
Show Gist options
  • Save batosai/2697373 to your computer and use it in GitHub Desktop.
Save batosai/2697373 to your computer and use it in GitHub Desktop.
Check if date is now
//Sample
NSDate *myDate = [[NSDate alloc] initWithString:@"Some date string"];
if ([[NSDate date] isSameDay:myDate]) {
NSLog(@"Today");
}
#import <Foundation/Foundation.h>
@interface NSDate (date)
- (BOOL)isSameDay:(NSDate*)anotherDate;
@end
#import "NSDate.h"
@implementation NSDate (date)
- (BOOL)isSameDay:(NSDate*)anotherDate
{
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components1 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
NSDateComponents* components2 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:anotherDate];
return ([components1 year] == [components2 year] && [components1 month] == [components2 month] && [components1 day] == [components2 day]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment