Supertecnoboff
Supertecnoboff

Reputation: 6606

Determine iDevice current date in Xcode - NSDate

I want to create a simple if function which carries out a bit of code depending on if the current iDevice year is 2012. I have tried to use NSCalendar and NSDateComponents in Xcode for this but have failed... :(

Anyone know of any good resources or blogs with tutorials/advice on this kind task?

Thanks,

Dan

Upvotes: 0

Views: 1708

Answers (1)

Kreiri
Kreiri

Reputation: 7850

NSDate *date = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:date];
int year = components.year;

Upvotes: 2

Related Questions