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.
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:
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.
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.
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.
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.
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
dateComponentswhen you need several values at once. - Use
DateFormatterfor display strings, not for core time logic. - Remember that
Dateis an absolute moment andCalendardecides how that moment becomes an hour value.
Related reading
- How to get the hour of the day with Swift?
- How to get the indexpath.row when an element is activated?
- How to get the indexpath.row when an element is activated?
- How to get the iPhone's screen width in SwiftUI?
- How to get the name of enumeration value in Swift?
- How to get the name of the running application in iOS
- How to get the Power of some Integer in Swift language?
- How to get the result of OnPostExecute to main activity because AsyncTask is a separate class?
.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.