2

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]])
Andrew
  • 3,874
  • 5
  • 39
  • 67
  • See Gabriele's answer below. I have a similar requirement in an app to check whether an arbitrary event (based on time) occurred on the same calendar date and this is exactly how I do it. Just compare by date components matching year, month and day. – Bladebunny Nov 02 '13 at 17:06
  • I checked that question before posting and the solutions weren't working – Andrew Nov 02 '13 at 22:04

1 Answers1

1

As a rule of thumb, when you compare or manipulate NSDates, the right tool for the job is usually a combination of NSCalendar and NSDateComponents.

Use NSDateFormatter only when you need to format and display a NSDate.

With this in mind, NSDateComponents is the way to go, since you want to compare a subset of components from the two dates.

Create two NSDateComponents instances and compare them.

Example:

- (BOOL)isToday:(NSDate *)date {
    NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                                                        fromDate:[NSDate date]];
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                                                       fromDate:date];
    return [dateComponents isEqual:todayComponents];
}
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235