Basically, I want to figure out if it's the next day. So, I'm storing the current date (e.g. Jan 2) constantly in a plist. But the next time the user opens the application, if the date has changed (e.g. Jan 3), I want to do something. Note that a simple ascending order check wouldn't work because I don't want to know if one date is later than another date, if the difference is only in hours. I need to be able to differentiate Jan 2 11:50 and Jan 3 2:34 but not Jan 3 2:34 and Jan 3 5:12.
+4
A:
I use the following that I found SO:
- (BOOL)isSameDay:(NSDate*)date1 otherDay:(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];
}
Niels Castle
2010-01-03 11:39:55
A:
Use NSCalendarDate instead of NSDate, and compare what they return from -dayOfCommonEra
@implementation NSDate (sameDayOrNot)
- (BOOL) sameDayAsDate:(NSDate *) otherDate
{
NSCalendarDate *date1 = [[NSCalendarDate alloc] initWithTimeInterval:0 sinceDate:self];
NSCalendarDate *date2 = [[NSCalendarDate alloc] initWithTimeInterval:0 sinceDate:otherDate];
BOOL result = ([date1 dayOfCommonEra] == [date2 dayOfCommonEra]);
[date1 release];
[date2 release];
return result;
}
@end
NSResponder
2010-01-03 11:51:37
NSCalendarDate is deprecated
Abizern
2010-01-03 11:54:39
Not yet, it's not.
NSResponder
2010-01-03 12:02:10
Uh, yes it is http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/LegacyNSCalendarDate.html Deprecated isn't the same as removed. Also, the question is not about checking the equivalence of two dates, but how to tell if they are on different days.
Abizern
2010-01-03 12:21:09
I checked the docs in Xcode (I'm running v 3.2.1), and what I see there says "It is not deprecated yet, however it may be in the next major OS release after Mac OS X v10.5." Looks like the docs in the Xcode packages aren't up to date.At any rate, my code above is still correct. Note that I'm comparing the return value from -dayOfCommonEra.
NSResponder
2010-01-03 14:32:14
Regardless of whether it actually is deprecated, NSCalendarDate has 1 big technical problem: it only handles the Gregorian calendar. NSCalendar is much better equipped, available on iPhone too, and has no warnings of deprecation (impending or otherwise).
Mike Abdullah
2010-01-03 22:41:13
Have you ever, in your entire career, needed to use any calendar other than the Gregorian? Even if you have, the question at hand was how to check if two dates fall on the same day or not, which doesn't depend on which calendar you're using.
NSResponder
2010-01-04 00:10:11
A:
Looking at the Date Time Programming Guide gives you an example of how to calculate the difference between two dates as by component: for example:
NSDate *startDate = ...;
NSDate *endDate = ...;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:startDate
toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];
Abizern
2010-01-03 11:52:17