Member-only story

How To Work With Dates in Swift

Artturi Jalli
Better Programming
Published in
3 min readApr 21, 2021

--

Drawing of code on device
Photo by the author.

According to Apple, a date is:

“A specific point in time, independent of any calendar or time zone.”

You can get the current date simply with:

let date = Date()
print(date)

Output:

2021-04-21 9:23:19 +0000

A Date object supports:

  • Comparison
  • Time interval calculation
  • Creating a new date with respect to another date.

For example:

Output:

3600.0
2021-04-20 05:18:45 +0000
2021-04-20 04:18:45 +0000

This is already something. But if you need to have more control over the dates, you should use Swift’s built-in DateFormatter.

DateFormatter—Customize and Localize Dates

--

--

Write a response