3

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Taier
  • 2,109
  • 12
  • 22
  • Have you checked this link? https://gist.github.com/dgyesbreghs/5e1ddbfb3e85643ba706 – aBilal17 Jul 02 '18 at 14:35
  • @aBilal17 as I can see it gives a NSDate instance of a first day of the week (but you need to set it manually, Monday or Sunday). The NSDate does not tell is it a monday or sunday... – Taier Jul 02 '18 at 14:38
  • 1
    "*Is it possible to determinate that using NSCalendar?*" - any reason you didn't look at the reference documentation for `NSCalendar/Calendar` before posting your question? – rmaddy Jul 02 '18 at 15:15
  • @maddy thanks, for some reason I overlook that property... – Taier Jul 02 '18 at 15:19

2 Answers2

13

It's just:

Swift:

Calendar.current.firstWeekday

Obj-C:

[NSCalendar currentCalendar].firstWeekday

With 1 = Sunday.

LorenzOliveto
  • 7,796
  • 1
  • 20
  • 47
2

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)
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • 1
    You should make it clear that if you want to know the current user's first day of the week, you do not need to doing anything with the locale. – rmaddy Jul 02 '18 at 15:13