Apollo
Apollo

Reputation: 9064

Going from numerical month to Gregorian name for month

Currently I'm using the following code to acquire a date from a date picker. Right now, when I NSLog the NSDateComponents var, I get the numberical representation for a month (i.e. 7 for July). How can I print out the Month name, as well as the day of the week, like Saturday?

   NSDate *selectedDate = _datePicker.date;

    NSDateFormatter *df = [[NSDateFormatter alloc] init];

    [df setDateFormat:@"MM:DD:HH:mm"];

    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:_datePicker.date];

    NSLog(@"components: %@",components);

Upvotes: 0

Views: 67

Answers (2)

yonosoytu
yonosoytu

Reputation: 3319

You might use monthSymbols from NSDateFormatter:

NSDate *selectedDate = _datePicker.date;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:selectedDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *month = [[dateFormatter monthSymbols] objectAtIndex:components.month - 1];

Upvotes: 2

一二三
一二三

Reputation: 21249

Use the format strings EEEE for the day of the week, and MMMM for the month of the year in your NSDateFormatter.

Upvotes: 2

Related Questions