Objective-c get the day of the week in a number starting on monday, not sunday
I am making my own app calendar for the European market. In the function, I have to get the number of the day of the week ... I do, but it returns the number of the day of the week starting from Sunday. How am I supposed to hard-code this number starting Monday? thanks Here's what I have so far:
-(int)getWeekDay:(NSDate*)date_
{
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:frLocale];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:date_];
int weekday = [comps weekday];
NSLog(@"Week day is %i", weekday);
return weekday;
}
+3
source to share
2 answers
The best way to do this is to use [NSCalendar setFirstWeekday:]
as Joshua said in his answer.
Otherwise, you can do integer arithmetic. The Vova method is simple:
if (weekday>1)
weekday--;
else
weekday=7;
This one is below, although a little confusing:
int europeanWeekday = ((weekday + 5) % 7) + 1;
+4
source to share