Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine day, month, and year as int in iOS

I want to determine day, month, and year of current date in iOS. I found this after a search on the net:

NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit) fromDate:date];
NSInteger Day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

This code shows the day correct, but the year and month is not showing as it should be. It shows some digits like 2147483647. I could not find a solution so please help me correct the code I found, or write some new code which does what I need.

like image 395
rudasagani Avatar asked Jul 12 '12 08:07

rudasagani


1 Answers

This line is the problem:

NSDateComponents *components = [calendar components:(NSDayCalendarUnit) fromDate:date];

It should be

NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
like image 136
sosborn Avatar answered Oct 13 '22 19:10

sosborn