Taier
Taier

Reputation: 2119

How to find out what first day of week is? (Monday or Sunday)

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

Answers (2)

Ashley Mills
Ashley Mills

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

LorenzOliveto
LorenzOliveto

Reputation: 7936

It's just:

Swift:

Calendar.current.firstWeekday

Obj-C:

[NSCalendar currentCalendar].firstWeekday

With 1 = Sunday.

Upvotes: 15

Related Questions