Essentially I have a tip of the day feature, with specific tips associated with dates, and I need a way to compare the current NSDate
to the saved date for the tip, regardless of time. Obviously the tip for January 1st is relevant at 10AM and 10:30, so I can't literally compare the NSDates
. Here are some of the things I've tried:
Try 1 - Doesn't work
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
// [df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
Try 2 - Doesn't work
NSString * zoneString = [[NSString stringWithFormat:@"%i-%i-%iT00:00:00", year, month, day] substringFromIndex:([[NSString stringWithFormat:@"%i-%i-%iT00:00:00", year, month, day] length] - 5)];
NSTimeInterval timeInterval = [[zoneString substringToIndex:3] intValue] * 3600;
timeInterval += [[zoneString substringFromIndex:3] intValue] * 60;
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:timeInterval]];
[formatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss ZZZ"];
NSLog(@"%@", [formatter stringFromDate:myDate]);
Try 3 - Doesn't work
NSDate *date = [NSDate date];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&date interval:NULL forDate:date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
if ([[dateFormat stringFromDate:date] isEqualToString:[dateFormat stringFromDate:tipDate]])