This code will take your string, convert it to an NSDate object and retrieve both the day number (14) and the day name (Sunday)
NSString *myDateString = @"2013-04-14"; // Convert the string to NSDate NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd"; NSDate *date = [dateFormatter dateFromString:myDateString]; // Extract the day number (14) NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:date]; NSInteger day = [components day]; // Extract the day name (Sunday) dateFormatter.dateFormat = @"EEEE"; NSString *dayName = [dateFormatter stringFromDate:date]; // Print NSLog(@"Day: %d: Name: %@", day, dayName);
Note. This code is for ARC. If MRC, add [dateFormatter release] at the end.
source share