Posted By

OreganoJones on 02/08/11


name weekday NSCalendar

Versions (?)

Return the weekday name from the weekday number.

 / Published in: Objective C

A name of a given day of the week. With the week day x, build an NSDate for the first x in February 2011. Format the NSDate so that the weekday name is generated. Is this really necessary, or is it a tragically roundabout detour?

  1. NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  3. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  4. [dateComponents setWeekday: x];
  5. [dateComponents setWeekdayOrdinal:1];
  6. [dateComponents setYear:2011];
  7. [dateComponents setMonth:2];
  9. NSDate *tempDate = [gregorian dateFromComponents:dateComponents];
  11. NSDateFormatter *format = [[NSDateFormatter alloc] init];
  12. [format setDateFormat:@"EEEE"];
  14. NSString *retval = [format stringFromDate:tempDate];

Report this snippet  

You need to login to post a comment.