Posted By

OreganoJones on 02/08/11


Tagged

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];
  2.  
  3. NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
  4. [dateComponents setWeekday: x];
  5. [dateComponents setWeekdayOrdinal:1];
  6. [dateComponents setYear:2011];
  7. [dateComponents setMonth:2];
  8.  
  9. NSDate *tempDate = [gregorian dateFromComponents:dateComponents];
  10.  
  11. NSDateFormatter *format = [[NSDateFormatter alloc] init];
  12. [format setDateFormat:@"EEEE"];
  13.  
  14. NSString *retval = [format stringFromDate:tempDate];

Report this snippet  

You need to login to post a comment.