Question

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.

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top