How to format date and time in Swift

3 Answers

0 votes
import Foundation

let today = Date()
let df = DateFormatter()

df.dateStyle = .short

print(df.string(from: today))




/*
run:
 
6/19/21
 
*/

 



answered Jun 19, 2021 by avibootz
0 votes
import Foundation

let today = Date()
let df = DateFormatter()

df.dateStyle = .medium

print(df.string(from: today))




/*
run:
 
Jun 19, 2021
 
*/

 



answered Jun 19, 2021 by avibootz
0 votes
import Foundation

let today = Date()
let df = DateFormatter()

df.dateFormat = "HH:mm E, d MMM y"

print(df.string(from: today))




/*
run:
 
06:38 Sat, 19 Jun 2021
 
*/

 



answered Jun 19, 2021 by avibootz
...