11

In order to get the days of the week I use:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray *weekdays = [dateFormatter shortWeekdaySymbols];

Weekdays gives me an array with the days names but it begins by Sunday. For some reasons I want this array to begin by Monday or Sunday depending on the localisation settings of the device.

Is there a way to do it?

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
Erwan
  • 131
  • 1
  • 6

5 Answers5

15

You can get the 1-based index of the first weekday of the current locale from the -firstWeekday method of an NSCalendar object with the current locale. Then you can modify your week names array accordingly:

// get week day names array
NSArray *weekdays = self.shortWeekdaySymbols;

// adjust array depending on which weekday should be first
NSUInteger firstWeekdayIndex = [NSCalendar currentCalendar].firstWeekday - 1;
if (firstWeekdayIndex) {

    NSRange firstRange = NSMakeRange(firstWeekdayIndex, weekdays.count - firstWeekdayIndex);
    NSRange lastRange = NSMakeRange(0, firstWeekdayIndex);

    NSArray *firstArray = [weekdays subarrayWithRange:firstRange];
    NSArray *lastArray  = [weekdays subarrayWithRange:lastRange];

    weekdays = [firstArray arrayByAddingObjectsFromArray:lastArray];
}

NSLog(@"%@", weekdays);

I don't have the iPhone SDK but AFAIK these APIs should be all available there and behave the same way as on OS X.

CarlJ
  • 9,461
  • 3
  • 33
  • 47
hasseg
  • 6,787
  • 37
  • 41
1

Achieved a result similar to hasseg in Swift using:

    var symbols = dateFormatter.shortWeekdaySymbols as! [String]
    let firstDayIndex = calendar.firstWeekday - 1

    if firstDayIndex > 0 {
        var sub = symbols[0..<firstDayIndex]
        symbols.removeRange(Range<Int>(start:0, end:firstDayIndex))
        symbols += sub
    }
Mark Leonard
  • 2,056
  • 19
  • 28
1

like the myexec solution but more generic and equally concise

let numDays = Calendar.current.weekdaySymbols.count
let first = Calendar.current.firstWeekday
let end = first + numDays - 1

let days = (first...end).map {Calendar.current.weekdaySymbols[$0 % numDays]}
Davide Candita
  • 386
  • 2
  • 9
  • 1
    Is it possible you meant `let first = Calendar.current.firstWeekday` (without decreasing 1)? I think otherwise you would still get strings starting from Sunday, even for a Monday-as-first-day locale. – Frankie Simon Aug 31 '21 at 13:23
  • 1
    You're right. I have corrected the answer – Davide Candita Sep 02 '21 at 09:35
0
for i in 0...6 {
    let day = Calendar.current.weekdaySymbols[(i + Calendar.current.firstWeekday - 1) % 7]
    print(day)
}
myexec
  • 470
  • 1
  • 4
  • 13
0

You need to use NSCalender to change the start day of your week from programatically.

NSCalendar *_calendar;
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
_calendar.timeZone = [NSTimeZone localTimeZone];
[_calendar setFirstWeekday:2];// Sunday == 1, Saturday == 7
_calendar.locale = [NSLocale currentLocale];

    NSDateComponents *dateComponent = [_calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:[NSDate date]];
    NSDate *now = [NSDate date];
    NSDateComponents *comp = [_calendar components:NSCalendarUnitYear fromDate:now];
    [comp setWeekOfMonth:dateComponent.weekOfYear];  //Week number.
    [comp setWeekday:2];

    NSDate * weekstartPrev = [_calendar dateFromComponents:comp];
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56