
How do you work with date and time in Swift? In this tutorial, we’ll discuss how you can datetime calculator online convert date/time to strings, and vice versa, how date/time formatting works, how to calculate time durations, and much more.
Get Current Date and Time With Swift
The default time for a newly initialized Date object, like the one above, is the current date and time. When you create a new Date object with the Date() initializer, it has the current date and time.
Let’s discuss this Date type some more. Here’s what you need to know:
The Date type is part of the Foundation framework, so it’s a fundamental type to work with dates on iOS, macOS, tvOS, watchOS and Catalyst.
A Date object represents a single point in time, regardless of locale or calendar setting. As you’ll soon see, this fact is crucial to work with dates effectively.
A Date object has millisecond precision, i.e. it can represent points in time up to 1/1000th of a second. The default time unit for date/time APIs is the second (and fractions), which is OK for common use cases.
Keep in mind that a Date object can be represented as a string, so it can be printed out, but that’s not the recommended approach to convert date to string (or vice versa). When you run the above code in Xcode, you’ll get the current date and time in ISO 8601 format (in your system’s timezone) as debug output.
If you’re familiar with working with date and time in app development, you may have heard of the term “Unix time” (or Unix timestamp). A Unix timestamp is (commonly) the number of seconds that have elapsed since January 1st, 1970 at 00:00:00 (midnight, UTC). Timestamps are commonly represented as 10-digit numbers, so it’s easy to store them in a database or simple Int or Double value.
But… there’s going to be a lot of “buts” in the rest of this tutorial. Because for every rule we establish, like “Unix timestamps fit in 10 digits”, there’s an exception you’ll have to take in account. Things like timezones, leap seconds, calendars, formatting and locales. It’ll be fun!
What’s going on? First, we got the current date and time. Then, we created a DateFormatter() object and provided it with values for 2 properties: dateStyle and timeStyle. Then, we called the function string(from:) on the date formatter, provided it the Date object, and printed out the resulting string datetime. Awesome!
You can choose from various styles with the dateStyle and timeStyle properties. Both take values from the DateFormatter.Style enum. You can combine dateStyle and timeStyle to include date or time or both.
The exact date format depend on a system’s locale, which is what we’ll discuss next. What’s a locale? In short, it’s a set of parameters that defines a user’s language, region (or country), and any additional settings or variations.
The above table was generated with the en_US locale, which means it uses the US English style of formatting date and time: AM/PM, and month/day/year. Compare this to different countries and locales around the world, that use a 24-hour clock, and day-month-year, and you see why locales are so important!
Leave a comment