Skip to content

Instantly share code, notes, and snippets.

@leeyspaul
Created December 11, 2021 01:01
Embed
What would you like to do?
DateFormatter Styles
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! :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment