Week numbers in Swift
To do week number calculations in Swift, you need configure a Calendar instance.
var calendar = Calendar(identifier: .gregorian)
calendar.firstWeekday = 2
calendar.minimumDaysInFirstWeek = 4
How to get the week number from a date
To get the ISO week number (1-53), use
calendar.component(.weekOfYear, from: .
To get the corresponding four-digit year (e.g. 2013), use
calendar.component(.yearForWeekOfYear, from: .
date is a Date instance.
Read more about
in the Swift documentation.
Week numbers in …
A lot of software that deal with dates has poor support for week number, in particular the ISO week numbering scheme that is used on this site.
The concept of week numbers is virtually unknown in large parts of the world, so adding support for them is not high on the agenda for most software companies.
However, with a few tricks it is often possible to make week numbers calculations, even if the application does not have native support for week numbers.
These pages explain how to use week numbers in various popular software applications and programming languages.