How do I get the day of the week with Foundation?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Foundation gives you two practical ways to get the day of the week from a date. You can ask Calendar for the numeric weekday component, or you can use DateFormatter when you need a localized weekday name such as “Monday” or an abbreviated form such as “Mon”.
Getting the Numeric Weekday with Calendar
If all you need is the weekday number, Calendar is the correct API.
The returned value is calendar-dependent. In the Gregorian calendar used by Foundation, 1 is Sunday, 2 is Monday, and so on.
That detail matters because developers sometimes expect Monday to be 1. Foundation can tell you the first weekday used by a calendar, but the numeric component itself still follows the calendar’s indexing rules.
Building a Specific Date Safely
In real code, you usually start from date components rather than a fixed Unix timestamp. Foundation makes that straightforward.
Using DateComponents avoids fragile string parsing and makes tests much clearer.
Getting the Localized Weekday Name
If you need a readable label, use DateFormatter.
EEEE returns the full weekday name. For abbreviated output, use EEE.
You can also ask the formatter for its weekday symbol arrays if you want to map numeric values to names yourself.
This is useful when building a calendar UI or a custom picker.
Calendar, Locale, and Time Zone Matter
Dates are not just numbers. The weekday depends on the calendar system and, in edge cases, on time zone.
If a date comes from a server timestamp, converting it in a different time zone may shift the calendar day and therefore the weekday. For example, a timestamp near midnight UTC may still be the previous day in Toronto or Los Angeles.
When reproducibility matters, set the time zone explicitly.
This avoids subtle “works on my machine” date bugs.
When to Use Each API
Use Calendar.component(.weekday, from:) when you need logic. Examples include checking whether a date is on a weekend or indexing into a custom weekly schedule.
Use DateFormatter when you need presentation. It handles localization and user-facing formatting more cleanly than manual string arrays in most cases.
In many apps, you use both: Calendar for decisions and DateFormatter for display.
Common Pitfalls
Assuming weekday numbers start at Monday is a frequent mistake. Check the calendar semantics instead of guessing.
Ignoring locale can make weekday names appear in the wrong language or style for the user.
Forgetting time zone handling can shift the date and therefore the weekday, especially when the input is an absolute timestamp.
Finally, avoid parsing arbitrary date strings manually when DateComponents, Calendar, and DateFormatter already provide safer building blocks.
Summary
- use
Calendar.component(.weekday, from:)to get the weekday number - use
DateFormatterwithEEEEorEEEto get a localized weekday name - weekday numbering depends on the calendar, not your intuition
- time zone and locale can change the result or its presentation
- use
Calendarfor logic andDateFormatterfor display
Related reading
- How do I get the dialer to open with phone number displayed?
- How do I get the key at a specific index from a Dictionary in Swift?
- How do I get the key at a specific index from a Dictionary in Swift?
- How do I get the RootViewController from a pushed controller?
- How do I get the SharedPreferences from a PreferenceActivity in Android?
- How do I handle ImeOptions' done button click?
- How do I hide a menu item in the actionbar?
- How do I hide the status bar in a Swift iOS app?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.