Return the weekday name from the weekday number.

/ Published in: Objective C
Save to your folder(s)

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?

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.