Foundation
day of the week
programming
Swift
date handling

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.

Browse interview questions

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.

swift
1import Foundation
2
3let calendar = Calendar(identifier: .gregorian)
4let date = Date(timeIntervalSince1970: 0)
5let weekday = calendar.component(.weekday, from: date)
6print(weekday)

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.

swift
1import Foundation
2
3var components = DateComponents()
4components.year = 2026
5components.month = 3
6components.day = 7
7
8let calendar = Calendar(identifier: .gregorian)
9let date = calendar.date(from: components)!
10let weekday = calendar.component(.weekday, from: date)
11print(weekday)

Using DateComponents avoids fragile string parsing and makes tests much clearer.

Getting the Localized Weekday Name

If you need a readable label, use DateFormatter.

swift
1import Foundation
2
3let formatter = DateFormatter()
4formatter.locale = Locale(identifier: "en_US")
5formatter.dateFormat = "EEEE"
6
7let calendar = Calendar(identifier: .gregorian)
8let date = calendar.date(from: DateComponents(year: 2026, month: 3, day: 7))!
9print(formatter.string(from: date))

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.

swift
1import Foundation
2
3let formatter = DateFormatter()
4formatter.locale = Locale(identifier: "en_US")
5print(formatter.weekdaySymbols)
6print(formatter.shortWeekdaySymbols)

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.

swift
1import Foundation
2
3var calendar = Calendar(identifier: .gregorian)
4calendar.timeZone = TimeZone(identifier: "America/Toronto")!
5
6let formatter = DateFormatter()
7formatter.calendar = calendar
8formatter.timeZone = calendar.timeZone
9formatter.locale = Locale(identifier: "en_CA")
10formatter.dateFormat = "EEEE"
11
12let date = Date(timeIntervalSince1970: 1772863200)
13print(formatter.string(from: date))

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 DateFormatter with EEEE or EEE to 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 Calendar for logic and DateFormatter for display

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.