How to Get Current Time in Swift: 5 Fatal Errors and Their Solutions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Time is Not Just Numbers: Why Even Experienced Developers Make Mistakes

\n\n

Every iOS developer has faced the task at least once: "I just need to get the current time." It seems simple enough, right? But right here, out of nowhere, beginners (and not only them) make mistakes that later turn into bugs, app crashes, and unhappy users.

\n\n

In this article, we'll break down five of the most common errors when working with the current time in Swift, which have gathered thousands of views on Stack Overflow. You'll learn how to avoid them and write clean, reliable code.

\n\n
"I just want to get the current time as datetime in Swift, but I either get nil or something I didn't expect. Help!" — a typical cry for help on forums.
\n\n

Let's figure out what's going wrong.

\n\n

1. DateFormatter Error: Locale is the Main Enemy

\n\n

The Problem

\n

The most popular query: "Swift get current time as datetime string". The developer writes:

\n\n
let dateFormatter = DateFormatter()\ndateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"\nlet currentTime = dateFormatter.string(from: Date())\nprint(currentTime) // Works? Not quite
\n\n

In the simulator or on a device with an English locale, everything is fine. But as soon as the user selects, for example, German or Russian in the settings, you get nil or an unexpected format.

\n\n

Why Does This Happen?

\n

By default, DateFormatter uses the device's current locale. For fixed date formats that are parsed or displayed in an API, this is a disaster.

\n\n

The Solution

\n

Always set the locale to en_US_POSIX for formats not intended for user display:

\n\n
let dateFormatter = DateFormatter()\ndateFormatter.locale = Locale(identifier: "en_US_POSIX")\ndateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"\ndateFormatter.timeZone = TimeZone.current // Don't forget the time zone!\nlet currentTime = dateFormatter.string(from: Date())\nprint(currentTime) // "2024-12-25 14:30:00" — stable
\n\n
Remember: en_US_POSIX is your best friend when working with fixed date formats in Swift.
\n\n

2. Ignoring the Time Zone (TimeZone)

\n\n

The Problem

\n

You got a string with the current time. But what time did you actually get? Local? UTC? Server time? The error occurs when the developer does not specify timeZone in DateFormatter.

\n\n

Example of an error:

\n
let formatter = DateFormatter()\nformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"\nlet now = formatter.string(from: Date())\n// Result: "2024-12-25T14:30:00+0300" — this is local time with offset\n// But what if you always need UTC?
\n\n

The Solution

\n

Always explicitly specify TimeZone:

\n\n
// For working with the server (UTC)\nlet utcFormatter = DateFormatter()\nutcFormatter.timeZone = TimeZone(abbreviation: "UTC")\nutcFormatter.locale = Locale(identifier: "en_US_POSIX")\nutcFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"\nlet utcTime = utcFormatter.string(from: Date())\nprint(utcTime) // "2024-12-25T11:30:00Z"\n\n// For displaying to the user (their local time)\nlet localFormatter = DateFormatter()\nlocalFormatter.timeZone = TimeZone.current\nlocalFormatter.dateStyle = .medium\nlocalFormatter.timeStyle = .short\nlet localTime = localFormatter.string(from: Date())\nprint(localTime) // "Dec 25, 2024 at 2:30 PM"
\n\n

3. Using Date() for Comparison: Pitfalls

\n\n

The Problem

\n

Many think that Date() returns the same value within a single call. But this is not true. Swift Date is a snapshot of time. If you call Date() twice in your code, the values may differ by microseconds.

\n\n


                
                

Blogs

Book Recommendations