views:

125

answers:

2

Hi all,

I'm trying to return the 4 digit year from any given date as an integer. I'm not strong with the NSCalendar or NSDate functions, but have tried using them with components from date and date formatters with no success.

Thanks.

+1  A: 

From NSCalendar docs:

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];

Then you can get the year by doing:

NSInteger year = [comps year];
pgb
Thank you - that did it. The last line is what I had been missing; I didn't know how to reference the year specifically.
Mike W
A: 

Where is "gregorian" defined????

Susanna