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

I want to determine the day, month and year of the current date in iOS. I found this after searching 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 is correct, but the year and month are not displayed as it should be. It shows some numbers, such as 2147483647. I could not find a solution, so please help me fix the code I found, or write a new code that does what I need.

+6
source share
1 answer

This line is the problem:

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

It should be

 NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date]; 
+13
source

Source: https://habr.com/ru/post/920298/


All Articles