views:

24

answers:

1

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?

A: 

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
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray *weekdays = [dateFormatter shortWeekdaySymbols];

// adjust array depending on which weekday should be first
NSUInteger firstWeekdayIndex = [[NSCalendar currentCalendar] firstWeekday] - 1;
if (firstWeekdayIndex > 0)
{
    weekdays = [[weekdays subarrayWithRange:NSMakeRange(firstWeekdayIndex, 7-firstWeekdayIndex)]
                arrayByAddingObjectsFromArray:[weekdays subarrayWithRange:NSMakeRange(0,firstWeekdayIndex)]];
}

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.

hasseg