NSDate
Swift programming
iOS development
date manipulation
weekday calculation

Get day of week using NSDate

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To get the day of the week from an NSDate, you do not ask the date directly. NSDate only represents a moment in time. The weekday comes from interpreting that moment through a calendar, a time zone, and sometimes a locale.

Use Calendar to extract the weekday

In modern Swift, NSDate bridges to Date, and Calendar does the real work. The numeric weekday can be read with .weekday:

swift
1import Foundation
2
3let nsDate = NSDate()
4let date = nsDate as Date
5
6let calendar = Calendar(identifier: .gregorian)
7let weekday = calendar.component(.weekday, from: date)
8
9print(weekday)

For the Gregorian calendar used in most iOS apps, the weekday values are usually:

  • '1 for Sunday'
  • '2 for Monday'
  • through 7 for Saturday

That numbering is easy to forget, so it is worth documenting anywhere the value is persisted or sent over an API.

Getting the weekday name

If you need "Monday" instead of 2, use DateFormatter. This respects the selected locale:

swift
1import Foundation
2
3let nsDate = NSDate()
4let formatter = DateFormatter()
5formatter.locale = Locale(identifier: "en_US_POSIX")
6formatter.dateFormat = "EEEE"
7
8let name = formatter.string(from: nsDate as Date)
9print(name)

"EEEE" gives the full weekday name. If you want a short form such as "Mon", use "EEE" instead.

Time zone matters more than many people expect

The same absolute timestamp can be one weekday in Toronto and another in Tokyo. If the app logic depends on a specific business time zone, set it explicitly instead of trusting device defaults.

swift
1import Foundation
2
3let formatter = ISO8601DateFormatter()
4let date = formatter.date(from: "2025-09-23T23:30:00Z")!
5
6var calendar = Calendar(identifier: .gregorian)
7calendar.timeZone = TimeZone(identifier: "America/Toronto")!
8
9let weekday = calendar.component(.weekday, from: date)
10print(weekday)

This is especially important for scheduling apps, billing cutoffs, and systems that group data by local day.

Objective-C version

If your codebase is still Objective-C-heavy, the same idea applies:

objective-c
1NSDate *date = [NSDate date];
2NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
3NSInteger weekday = [calendar component:NSCalendarUnitWeekday fromDate:date];
4NSLog(@"%ld", (long)weekday);

The API changes slightly, but the concept is identical: date plus calendar produces weekday.

When you need stable weekday labels

If the app uses weekday names for display, avoid hard-coding English names unless that is a product requirement. DateFormatter can localize names correctly, and Calendar exposes weekday symbol arrays when you want the names associated with the active calendar.

This is safer for apps that support multiple regions because "Monday" is not just a translation problem. The first day of the week, the calendar system, and user expectations can all vary by locale.

NSDate versus Date

If you see older documentation using NSDate, do not worry. In Swift, Date is the value-type bridge around the same Foundation concept. The real source of confusion is usually not NSDate itself, but mixing up:

  • absolute time
  • calendar interpretation
  • locale formatting
  • time zone conversion

Once those roles are clear, weekday extraction becomes straightforward.

Common Pitfalls

  • Expecting NSDate alone to know the weekday without a calendar.
  • Forgetting that .weekday numbering depends on the calendar convention and is not always Monday-first.
  • Using the device time zone when business logic needs a fixed time zone.
  • Using a localized formatter for internal logic, then getting different names on different devices.
  • Treating Date and NSDate as separate time models when they are bridged representations of the same value.

Summary

  • Use Calendar.component(.weekday, from:) to get the numeric weekday.
  • Use DateFormatter with "EEEE" or "EEE" when you need a weekday name.
  • Set calendar and time zone explicitly when correctness matters.
  • Remember that NSDate is just a timestamp; the weekday comes from calendar interpretation.
  • Prefer modern Swift Date, but the same approach works with bridged NSDate values.

Course illustration
Course illustration

All Rights Reserved.