I’m getting some data from a webserver. Within this data there is a date (i.e. 11-07-1978). I’d like to get the month as an integer like (int month = 7).
I have tried to make an NSDate from the String and get the month as a component with NSDateComponents. However I’m only getting the value 1.
Here’s a snippet of my code:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSDate *dateFromString = [dateFormatter dateFromString:[[NSUserDefaults standardUserDefaults] objectForKey:@"result_expdate"]];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:dateFromString];
int day = [components day];
int month = [components month];
int year = [components year];
NSLog(@"month = %d", month);
How can I get the month as an integer?
Here is a quick and dirty way of doing it-
this is hand typed so there may be typo’s…