Better Programming

Advice for programmers.

Follow publication

Member-only story

How To Work With Dates in Swift

Everything you need to know about working 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

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

No responses yet

Write a response