How to get the current date in SwiftUI for views

In SwiftUI there are two handy abstractions available to you to format dates for your views.

  1. Date
  2. DateFormatter

You can think of Date as the data piece of it giving you information on the current year, month, time, timezone etc. while you can think of DateFormatter as the piece that will give you that data in some prettified way that you want. In general to format a date for a view you first create a Date struct, then apply styles through the DateFormatter as a string or you can take a string and turn back into a Date to work with programatically.

Getting date data with Date

// creates the Date object which gives to you the current date in your systems locale.
let date: Date = Date()
view raw date.swift hosted with ❤ by GitHub

The above defines a Date struct and instantiates it. Once instantiated that struct has information on the specific date and time that is most current in a specific locale (locale basically means the time standards of where the user is, so if you’re in the U.S. it’ll show you a date format that is used in the U.S.). If you print out the object it’ll show you a specific date and time like 2021-12-08 21:33:29 +0000.

Formatting date data with DateFormatter

Once you have the Date struct you can format it nicely with DateFormatter which is a class you’ll instantiate and then apply the styles you’d like. There’s two ways to apply styles which is either

  1. you apply the given styles offered by Apple
  2. you write a custom date format string and apply it

Applying built-in date formats

DateFormatter comes with a total of 5 different formats. They do depend on the system locale, user preferences and OS settings but I will show the results from where I am based (U.S.) in the snippet below.

import Foundation
let date = Date()
let df = DateFormatter()
// FULL STYLE
df.dateStyle = DateFormatter.Style.full
df.timeStyle = DateFormatter.Style.full
print(df.string(from: date)) // Friday, December 10, 2021 at 5:00:41 PM Pacific Standard Time
// LONG STYLE
df.dateStyle = DateFormatter.Style.long
df.timeStyle = DateFormatter.Style.long
print(df.string(from: date)) // December 10, 2021 at 5:00:41 PM PST
// MEDIUM STYLE
df.dateStyle = DateFormatter.Style.medium
df.timeStyle = DateFormatter.Style.medium
print(df.string(from: date)) // Dec 10, 2021 at 5:01:29 PM
// SHORT STYLE
df.dateStyle = DateFormatter.Style.short
df.timeStyle = DateFormatter.Style.short
print(df.string(from: date)) // 12/10/21, 5:00 PM
// NONE STYLE
df.dateStyle = DateFormatter.Style.none
df.timeStyle = DateFormatter.Style.none
print(df.string(from: date)) // nothing! 🙂
view raw df.swift hosted with ❤ by GitHub

You can see that the different enum options from the Styles enum on DateFormatter shows various formats. You can pick and choose what works best for you to render to the user.

Of course, sometimes, none of these apply and you need your own custom date formats.

Applying custom date formats

When you need custom date formats that’s where the .dateFormat property comes handy. You can define your own date format through a string similar to how you would in other languages. For a list of different styles I suggest the resource on date format patterns on the unicode site. It could look like the below

import Foundation
let date = Date()
let df = DateFormatter()
df.dateFormat = "MMM d, yyyy" // prints out "Dec 10, 2021"
print(df.string(from: date))
view raw dfexample.swift hosted with ❤ by GitHub

Got any other cool date knowledge?

Come tweet it at me and let me know 🙂 @paulyoungsuklee.

Published by Paul Young-Suk Lee

SWE @lyft. Currently working on data infrastructure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: