-1

I used

[currentComps setFirstWeekday:2];

It returns the number of the day of week starting with Sunday. How should I hardcode returning this number starting with Monday?

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
Anu
  • 89
  • 5
  • Your question is unclear. `setFirstWeekday:` is a method of `NSCalendar`, not `NSDateComponents`, so your code cannot compile. - In the Gregorian calendar, 1 = Sunday, 2 = Monday, ... is fixed, you cannot change that. With `[calendar setFirstWeekday:2]` you can specify that Monday is considered as "start of the week" in all calculations. That does not change the fact that 2 = Monday. – Martin R Sep 05 '13 at 11:28
  • possible duplicate of [NSCalendar first day of week](http://stackoverflow.com/questions/1106943/nscalendar-first-day-of-week) – Juan Catalan Sep 05 '13 at 12:06

1 Answers1

0

Contrary to the comment by Martin R, you can use NSCalendar to specify the first day of the week via:

- (void)setFirstWeekday:(NSUInteger)weekday

Reading the documentation of NSCalendar, it states the method setFirstWeekday:

Sets the index of the first weekday for the receiver.

I haven't tried this myself, but I would assume that creating an instance of NSCalendar, calling the aforementioned method and also calling setCalendar: on your NSDateComponents would attenuate all dates components returned to work as you intend.

Zack Brown
  • 5,990
  • 2
  • 41
  • 54
  • 1
    I am not exactly sure what you mean by *"... all dates components returned to work as you intend"*, but Sunday is **always** represented as `1` in the `weekDay` property of `NSDateComponents` (assuming the Gregorian calendar). - The purpose of `setFirstWeekday:` is to determine the "start of the week" in calendrical calculations, for example `[calendar rangeOfUnit:NSWeekCalendarUnit ...]` to compute the beginning of a week. Some regions consider Monday as the first day in the week, other regions have Sunday as the first day in the week. But in any case: Sunday = 1, Monday = 2. – Martin R Sep 05 '13 at 12:39