DateFomatter in Swift
Apple Says
- A formatter that converts between dates and their textual representations.
In simple word
- It is a class that can take a Date, and output a String describing that time/date as its format instructions dictate. In other words, it has a few pretty useful built-in formats, and the capability to accept a custom date format string.
- It helps to convert date to string and also helps from string to date
Example of Date to String
let date : Date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let todaysDate = dateFormatter.string(from: date) //Output will be 05/04/2019
Example of String to Date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let dateObj = dateFormatter.dateFromString("05/04/2019")
More formatter
Sunday, Apr 07, 2019 --> EEEE, MMM d, yyyy
07/04/2019 --> MM/dd/yyyy
07-04-2019 14:11 --> MM-dd-yyyy HH:mm
Apr 07, 2:11 PM --> MMM d, h:mm a
April 2019 --> MMMM yyyy
Apr 07, 2019 --> MMM d, yyyy
Sun, 07 Apr 2019 14:11:54 +0000 --> E, d MMM yyyy HH:mm:ss Z
2019-07-12T14:11:54+0000 --> yyyy-MM-dd'T'HH:mm:ssZ
07.04.19 --> dd.MM.yy
10:41:02.112 --> HH:mm:ss.SSS
Thanks for reading, Happy Coding 👨💻