Swift
iOS Development
Time Management
Programming Guide
Swift Date Formatting

How to get the hour of the day with Swift?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

Getting the hour of the day in Swift is usually a one-line operation with Calendar. The important part is not the syntax itself, but whether you want the hour in the user’s current calendar and time zone or in a specific fixed time zone such as UTC.

For most app code, the right approach is to ask a Calendar for the .hour component of a Date. Once you understand that Date is an absolute moment and Calendar decides how to interpret it, the API becomes predictable.

The Simple Current-Hour Case

To get the hour for the current moment in the user’s current settings:

swift
1import Foundation
2
3let now = Date()
4let hour = Calendar.current.component(.hour, from: now)
5print(hour)

This returns an integer from 0 to 23 using the current calendar and time zone.

Getting the Hour From Any Date

You can extract the hour from any Date, not just the current one.

swift
1import Foundation
2
3let formatter = ISO8601DateFormatter()
4let date = formatter.date(from: "2026-03-11T18:45:00Z")!
5let hour = Calendar.current.component(.hour, from: date)
6print(hour)

The result depends on the current calendar and time zone. The same absolute Date may produce different hour values in different regions.

Use a Specific Time Zone When Needed

If you need the hour in UTC or another known time zone, create a calendar and set its timeZone explicitly.

swift
1import Foundation
2
3let formatter = ISO8601DateFormatter()
4let date = formatter.date(from: "2026-03-11T18:45:00Z")!
5
6var utcCalendar = Calendar(identifier: .gregorian)
7utcCalendar.timeZone = TimeZone(secondsFromGMT: 0)!
8
9let utcHour = utcCalendar.component(.hour, from: date)
10print(utcHour)

This avoids subtle bugs where code appears correct locally but breaks in another time zone.

Extract Multiple Components Together

If you need more than the hour, pull several components in one call.

swift
1import Foundation
2
3let now = Date()
4let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: now)
5print(components.hour ?? -1)

This is helpful when building time-based UI or scheduling logic.

Date Versus Display Formatting

Do not confuse extracting the hour numerically with formatting the hour for display.

If you want the numeric hour for logic, use Calendar.component(.hour, from:).

If you want a localized string such as 6 PM or 18:00, use DateFormatter instead.

swift
1import Foundation
2
3let formatter = DateFormatter()
4formatter.dateFormat = "HH"
5print(formatter.string(from: Date()))

That returns a string, not an integer. It is useful for UI, but less appropriate for control flow.

24-Hour Value Versus User Expectations

Calendar.component(.hour, from:) gives you the hour as a numeric component, commonly in a 24-hour range. If your UI must follow the user’s locale preferences for 12-hour or 24-hour display, use a formatter for presentation and keep the numeric hour for logic only.

This separation prevents bugs where display concerns leak into scheduling logic.

Common Pitfalls

A common mistake is assuming Date already contains a visible hour independent of calendar and time zone. Date is just an absolute instant.

Another mistake is using DateFormatter to extract an hour for business logic when Calendar.component would be clearer and less formatting-dependent.

Developers also forget to set a specific time zone when a backend protocol or scheduling rule requires UTC.

Finally, be careful around daylight saving transitions. If the exact interpretation of local time matters, test those edge cases explicitly.

Summary

  • Use Calendar.current.component(.hour, from: Date()) for the current local hour.
  • Use a custom calendar with an explicit time zone when the hour must be interpreted in UTC or another zone.
  • Use dateComponents when you need several values at once.
  • Use DateFormatter for display strings, not for core time logic.
  • Remember that Date is an absolute moment and Calendar decides how that moment becomes an hour value.

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.