-3

Possible Duplicate:
Convert NSString of a date to an NSDate

I need some help with formatting dates.

I have a string in the format 2011-10-05. How do i convert this NSString into NSDate object.

Furthermore I want to retrieve the day from the corresponding NSDate object.

I know I have to use a dateFormatter but I am not able to work around it. Help would be greatly appreciated.

Community
  • 1
  • 1
Neelesh
  • 3,673
  • 8
  • 47
  • 78

1 Answers1

2
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSDate *date = [dateFormatter dateFromString:@"2011-10-05"];
NSLog(@"date: %@", date);

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit fromDate:date];
NSInteger day     = [dateComponents day];
NSLog(@"day: %d", day);

NSLog output:

date: 2011-10-05 04:00:00 +0000
day: 5
zaph
  • 111,848
  • 21
  • 189
  • 228