Archive for 11th January 2012

It’s “ordinal date”, actually…

I’m storing birthdays in Core Data, and now I want to get a list of entities sorted by the day of their birth. However, I just cannot sort by the date, since the year of their birth would take precedence, I just need to sort by the month and day of the NSDate stored as the birth date.

The big problem that I faced was trying to find out how to get the Julian date from an NSDate. Well as it turns out, Julian date does not correctly describe what I was looking for, I needed to look for ordinal date.

After a bit of investigation, I was able to put together this category that I have hung off of NSDate in my app:

- (int)ordinalDate
{
    return [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit 
             inUnit:NSYearCalendarUnit forDate:self];
}

This, along with a sortUsingComparator of my NSMutableArray of contacts, is exactly what the doctor ordered.

BTW, Happy Birthday to the Grand Canyon National Monument, created this day in 1908, which eventually became the Grand Canyon National Park. (Yet another must see place.)