Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is Calendar.current.firstWeekday == 2?

In my playground, Calendar.current.firstWeekday == 1, which I interpret as

"Monday is the first day of the week where I live (Sweden)"

But when I run the simulator, Calendar.current.firstWeekday == 2, and I don't understand why! (In the simulator's settings I have set the region to "Sweden").

let c = Calendar.current
print(c) // prints "gregorian"
let first = Calendar.current.firstWeekday // this is 2 !!!

Question: Why is Calendar.current.firstWeekday == 2 and how can I fix this?

like image 253
ragnarius Avatar asked Feb 25 '17 17:02

ragnarius


2 Answers

which I interpret as

I believe your interpretation is wrong. I'm not sure how the framework assigns the days of the week, but from your question I believe that they go 1 through 7, where 1 is Sunday, 2 is Monday and so on. These are static and don't change with your settings.

The firstWeekday property tells you what day of the week the week starts in your locale. In the US, which is the default locale, a week starts on Sunday.

You can't "fix this" because nothing is wrong, that property is there to provide you with information which is specially useful when drawing a calendar.

like image 93
EmilioPelaez Avatar answered Oct 13 '22 00:10

EmilioPelaez


In the playground, the locale is set, as a default, to the US localization. Hence, it says that the first day of the week is 1/Sunday, which is the case in the US.

When you ran the code in the simulator, that HAD been set to Sweeden, you're told that the first day of the week is 2/Monday, which is the case in Sweden.

The discrepancy is due to the fact that the playground defaults to a US localization. If you'd like the playground to act as though it is in Sweden, you'll have to put that in code.

like image 41
Bryan Avatar answered Oct 13 '22 00:10

Bryan