Pages

July 10, 2013

Get current year

How to get current year in Objective-C using the Cocoa framework?
It works with both iOS (iPhone, iPad, iPod) and Mac OS systems.


/** Get current year.

 @return Returns the current year as integer.

 */
+ (NSInteger)getCurrentYear {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]];
    return [components year];
}



Download the ready-for-use source file (.m) of Get current year

No comments:

Post a Comment