NSDate
date comparison
iOS development
Swift programming
date functions

How to determine if an NSDate is today?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

To check whether an NSDate is today, use the current calendar rather than comparing raw timestamps. "Today" depends on calendar rules and time zone, so a direct equality check between two date objects is the wrong tool.

In modern Swift, Date and Calendar are the preferred APIs, but NSDate bridges cleanly to them. The easiest answer is Calendar.current.isDateInToday.

swift
1import Foundation
2
3let nsDate: NSDate = NSDate()
4let isToday = Calendar.current.isDateInToday(nsDate as Date)
5
6print(isToday)

That method is concise and handles calendar-aware logic correctly.

Why Raw Equality Fails

Two Date values include the full timestamp down to fractional seconds. Even if both fall on the same calendar day, they will almost never be exactly equal.

swift
1import Foundation
2
3let now = Date()
4let oneHourAgo = now.addingTimeInterval(-3600)
5
6print(now == oneHourAgo) // false
7print(Calendar.current.isDateInToday(oneHourAgo)) // usually true

The second line asks the right question: are both timestamps inside the current day according to the current calendar?

Comparing Day Components Manually

If you want more control, compare the year, month, and day components directly.

swift
1import Foundation
2
3let calendar = Calendar.current
4let candidate = NSDate(timeIntervalSinceNow: -7200) as Date
5
6let todayParts = calendar.dateComponents([.year, .month, .day], from: Date())
7let candidateParts = calendar.dateComponents([.year, .month, .day], from: candidate)
8
9let isToday =
10    todayParts.year == candidateParts.year &&
11    todayParts.month == candidateParts.month &&
12    todayParts.day == candidateParts.day
13
14print(isToday)

This manual approach is useful when you need to compare against a specific calendar or inspect the parts for other logic.

Comparing With a Specific Time Zone

Calendar.current uses the current locale and time zone settings. If your app has to define "today" in a fixed business time zone, create a calendar explicitly.

swift
1import Foundation
2
3var calendar = Calendar(identifier: .gregorian)
4calendar.timeZone = TimeZone(identifier: "America/Toronto")!
5
6let value = NSDate() as Date
7let isTodayInToronto = calendar.isDateInToday(value)
8
9print(isTodayInToronto)

That distinction matters in apps with servers, remote users, or scheduled jobs near midnight.

Older Foundation Style

In legacy Objective-C or older Swift code, you may see NSCalendar and NSDateComponents. The logic is the same even if the syntax is older.

swift
1import Foundation
2
3let calendar = NSCalendar.current
4let date = NSDate()
5
6if calendar.isDateInToday(date as Date) {
7    print("Today")
8}

The newer Date and Calendar APIs are still preferred for fresh code, but bridging from NSDate is seamless.

That makes migration easier in mixed codebases. You do not need to rewrite every NSDate call site immediately just to use the safer day-comparison helpers.

Common Pitfalls

The most common mistake is comparing timestamps directly instead of asking the calendar whether the date is in today.

Another issue is ignoring time zones. A date that is today for the user may already be tomorrow on the server, or the reverse, depending on how you define the feature.

Daylight saving transitions can also cause surprising behavior if you try to compute day boundaries manually with fixed numbers of seconds. Let Calendar do that work.

Finally, be consistent about whether "today" means the device calendar, a business calendar, or a server-side calendar. Those are different rules, and mixing them leads to hard-to-diagnose bugs.

Unit tests around midnight and daylight saving transitions are worth adding for any feature that drives reminders, streaks, or daily summaries.

Summary

  • Use Calendar.current.isDateInToday(nsDate as Date) for the simplest correct answer.
  • Do not compare NSDate values directly when the real question is about calendar days.
  • Use explicit calendars and time zones when your app defines "today" in a fixed region.
  • Manual component comparison is fine when you need extra control.
  • Let Calendar handle day boundaries instead of subtracting raw seconds.

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.