Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

nscalendar

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?

like image 894
Taier Avatar asked Oct 17 '25 11:10

Taier


2 Answers

It's just:

Swift:

Calendar.current.firstWeekday

Obj-C:

[NSCalendar currentCalendar].firstWeekday

With 1 = Sunday.

like image 129
LorenzOliveto Avatar answered Oct 19 '25 02:10

LorenzOliveto


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)
like image 24
Ashley Mills Avatar answered Oct 19 '25 01:10

Ashley Mills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!