Reputation: 2119
I want to check if the first day of the week for a user is Monday or Sunday to perform some actions with the calendar. Is it possible to determinate that using NSCalendar
or any other way?
Upvotes: 4
Views: 2368
Reputation: 53101
You can just use the firstWeekday
property…
var calendar = Calendar.current
calendar.locale = Locale(identifier: "en_GB")
print("\(calendar.locale!) starts on day \(calendar.firstWeekday)")
// en_GB starts on day 2
calendar.locale = Locale(identifier: "en_US")
print("\(calendar.locale!) starts on day \(calendar.firstWeekday)")
// en_US starts on day 1
update
Per @maddy's comment below, Calendar.current
will have the correct locale
set for the current user.
let calendar = Calendar.current
print("\(calendar.locale!) starts on day \(calendar.firstWeekday)")
// en_GB starts on day 2 (in my case)
Upvotes: 2
Reputation: 7936
It's just:
Swift:
Calendar.current.firstWeekday
Obj-C:
[NSCalendar currentCalendar].firstWeekday
With 1 = Sunday.
Upvotes: 15