The app I’ve been working lately makes a bit more use of NSDate than my usual ones. I thought I’d share with the world a few pieces of code I’ve been using;
How to test if two NSDate dates belong to the same day:
I created a category on NSDate and added the following method to it:
-(BOOL)isSameDay:(NSDate *) otherDate
{
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comps_self = [[NSCalendar currentCalendar] components:unitFlags fromDate:self];
NSDateComponents *comps_other = [[NSCalendar currentCalendar] components:unitFlags fromDate:otherDate];
if ([comps_self day]==[comps_other day] &&
[comps_self month]==[comps_other month] &&
[comps_self year]==[comps_other year]) {
return YES;
}
return NO;
}
I can now call [mydate isSameDay:otherdate]
Find out the first and last days in the month that contains a given date
-(void) updateMonthIntervalForDate:(NSDate*)date{
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* dc = [[NSCalendar currentCalendar] components:unitFlags fromDate:date];
dc.day=1;
NSDate* startOfMonth = [[NSCalendar currentCalendar] dateFromComponents:dc];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
offsetComponents.month=1;
NSDate* endOfMonth = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:self.startVisibleDate options:0];
// ... do whatever you want with startOfMonth and endOfMonth
}
Last but not the least, print out nicely Print out the full name of the month, or the abridged name
First of all, create a NSDateFormatter; the docs says it’s not inexpensive, so if you need it several times, try and reuse it instead of recreating it over and over again.
dateFormatter = [[NSDateFormatter alloc] init];
Next, decide the date format you want to output
Abridged version (like 01 Jan)
[dateFormatter setDateFormat:@"dd MMM"];
Full month name (January)
[dateFormatter setDateFormat:@"MMMM"];
Sure, this is in the docs as well, but I did quite a bit of searches to find out the part about MMMM for the full NSDate month name. So maybe it will help someone.
Once you have the date formatter ready, you can invoke it to get the nice string from your NSDate:
NSString* mystring = [[dateFormatter stringFromDate:crtEntry.date] uppercaseString];
This was it. I wish you happy coding.

Whatever happened to
return (a == b)
instead of the wasteful
if (a == b) {
return YES:
}
return NO;
There’s a wwdc video about writing better code that I recommend – I don’t have the link right now, though
Bottom line is, for your own sake( and others’), you should always chose clarity – make the purpose of your code clear at first sight. Compressing the if in a single line would save you a couple of lines of code, but three months from now, when you look at that condensed code, it would look messy and cluttered and take you a minor but still tiring mental effort to comprehend.
You are not coding ASM, so let the compiler do the minor optimizations for you; it will do a great job more often than not
May I recommend the incredibly useful NSDate-Utilities by Erica Sadun: https://github.com/erica/NSDate-Extensions
Thanks, that’s one great resource. Wish I’d known about it earlier, would have saved me a few hours of searching and coding!