views:

746

answers:

4

I need to know if two NSDate instances are both from the same day.

Is there an easier/better way to do it than getting the NSDateComponents and comparing day/month/year?

+3  A: 

NSDateComponents sounds like the best bet to me. Another tactic to try is toll-free-bridging it to a CFDate, then using CFDateGetAbsoluteTime and doing a subtraction to get the amount of time between the two dates. You'll have to do some additional math to figure out if the time difference lands the dates on the same day, however.

fbrereto
ya, but then you'd have to take into account so many things with regards to processing the time that you end up writing more code than just using NSDateComponents.
pxl
Right; I was suggesting an alternative to NSDateComponents, but still believe it is the best bet. If this becomes a performance-critical section of code the alternative may be slightly faster, but I'd avoid that optimization until proven necessary.
fbrereto
how expensive is NSDateComponents?
pxl
Not sure; measurements would have to be done to see how much of an improvement it'd be to migrate away from it.
fbrereto
`NSDate` has this nice method: `timeIntervalSinceDate:` Therefore you don't even need corefoundation.
Georg
+4  A: 

I just use a date formatter:

NSDateFormatter *dateComparisonFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateComparisonFormatter setDateFormat:@"yyyy-MM-dd"];

if( [[dateComparisonFormatter stringFromDate:firstDate] isEqualToString:[dateComparisonFormatter stringFromDate:secondDate]] ) {
    …
}

HTH.

Ben Lachman
that's even worse than using NSDateComponents, since the formatter has to first break the date into components anyway, and then there's creating two strings, and then comparing strings.
Prody
What's your metric for "better"? I think this is "easier" than the comparable NSDateComponents code in that it is more concise and is easily maintainable.
Ben Lachman
I've been using the dateComponents method, but this is much smarter. Shorter code is better, and if performance is a problem I can cache the NSDateFormatter.
Steven Fisher
+2  A: 

I use NSDateComponents to strip out the time aspect and then compare. Something like:

if ([[self beginningOfDay:date1] isEqualToDate:[self beginningOfDay:date2]]) 
{
...
}

- (NSDate *)beginningOfDay:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *comp = [calendar components:unitFlags fromDate:date];

    return [calendar dateFromComponents:comp];
}
Bruce Geerdes
beginningOfDay would make a nice category on NSDate...
Kendall Helmstetter Gelner
That uses two conversions for each date (components:fromDate and dateFromComponents). I use only the components:fromDate, extract the month/day/year from each and compare those 3 ints to see if they match.
progrmr
+4  A: 

NSDateComponents is my preference. How about something like this:

- (BOOL)isSameDay:(NSDate*)date1 (NSDate*)date2 {
    NSCalendar* calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];

    return [comp1 day]   == [comp2 day] &&
           [comp1 month] == [comp2 month] &&
           [comp1 year]  == [comp2 year];
}
progrmr