DateFormatter() A Deep Vision
All iOS developers struggles with date formatter even though they go through a lot. Some times it behaves like an adorable kid can’t stay under control. So all we need is a deep vision in DateFormatter
.
ALL WE NEED IS DATEFORMATTER
If we want process a Date
, All we need is Apple’s DateFormatter
class that has every thing we need. Here we will see how Date
and Date Strings
had been processed in desired way.
Here are someways to process date:
- Converting a
Date Strings
to aDate
using one of the date formats of dateFormatter instance. - Converting a
Date
to aDate Strings
using one of the date formats of dateFormatter instance. - Converting a
Date
instance to aDate Strings
using a user defined format. - Converting
Date Strings
a instance to aDate
using a user defined format. - Get
Date
from built-inDate Styles
.
Formatter Language
- yyyy — Defines year (eg: 2021)
- MM — Defines Month in number(eg: 04)
- MMM — Defines Month in word short(eg: Oct)
- MMMM — Defines Month in word full(eg: October)
- dd — Defines Day of month in number(eg: 23)
- EEE — Defines Day of week in word short(eg: Thu)
- EEEE — Defines Day of week in word full (eg: Thursday)
- HH — Defines hrs of time in 24 hrs format(eg: 23:)
- mm — Defines minutes of time(eg: :30)
- HH: mm — Defines hrs and minutes in 24 hrs format(eg: 23: 30)
- hh:mm — Defines hrs and minutes in 12 hrs format(eg: 11:30)
- ss — Defines seconds of time
- hh:mm:ss — Defines time with hrs, minutes and seconds(eg: 11:30:21)
- a — Defines AM/PM format of time
- hh:mm:ss a — Eg(11:30:21 PM)
Date Strings to a Date and vice versa:
First initialise Date formatter. Then give string date format to date format of date formatter instance. Then using date from string built in method of date formatter with user defined format we can pass user defined string date as param.
yyyy-MM-dd: Date String to Date and vice versa
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = “yyyy-MM-dd”
let date = dateFormatter.date(from: dateString)
print(date!)
print(dateFormatter.string(from: date!))
output
2021–04–22 18:30:00 +0000 //date from string
2021–04–23 // again string from date
dd-MM-yyyy: Date String to Date
let dateString1 = “03–04–2021”
var dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = “dd-MM-yyyy”
let date1 = dateFormatter1.date(from: dateString1)
print(date1!)
print(dateFormatter1.string(from: date1!))
output
2021–04–02 18:30:00 +0000 // date from string
03–04–2021 // again string from date
EEE, dd MMM yyyy: Date and day from date string
let dateString2 = “Thu, 22 October 2015”
var dateFormatter2 = DateFormatter()
dateFormatter2.dateFormat = “EEE, dd MMM yyyy”
let date2 = dateFormatter2.date(from: dateString2)
print(date2!)
print(dateFormatter2.string(from: date2!))
output
2015–10–21 18:30:00 +0000//date from string
Thu, 22 Oct 2015 // again string from date
let dateObj = dateFormatter2.date(from: dateString2)
dateFormatter2.dateFormat = “MM-dd-yyyy”
print(dateFormatter2.string(from: dateObj!))
output
10–22–2015//date from string
Day string from Date String
let df = DateFormatter()
df.dateFormat = “YYYY-MM-dd”
let date3 = df.date(from: dateString)! //”2021–04–23" — Mentioned above
df.dateFormat = “EEEE”
print(df.string(from: date3))
output
Friday //Day of the given date
Day string, Month String from Date String
let df1 = DateFormatter()
df1.dateFormat = “YYYY-MM-dd”
let date4 = df1.date(from: dateString)!//”2021–04–23" — Mentioned above
df1.dateFormat = “EEEE, YYYY MMM dd”
print(df1.string(from: date4))
output
Friday, 2021 Apr 23 //Day of the given date
Current Time and date to string
let today = Date()
let formatter1 = DateFormatter()
formatter1.dateFormat = “HH:mm E, d MMM y”
print(formatter1.string(from: today))
output:
00:29 Thu, 15 Apr 2021//Gives curennt Date and time in specified format
BONUS😊
Date styles is a built-in method in date formatters. We will see here how those methods reacts with dates.
dateStyle: .long
let today1 = Date()
let formatter2 = DateFormatter()
formatter2.dateStyle = .long
print(formatter2.string(from: today1))
output
April 15, 2021 //long style date
dateStyle: .medium
let today2 = Date()
let formatter3 = DateFormatter()
formatter3.dateStyle = .medium
print(formatter3.string(from: today2))
output
Apr 15, 2021
dateStyle: .medium
let today3 = Date()
let formatter4 = DateFormatter()
formatter4.dateStyle = .short
print(formatter4.string(from: today2))
output
4/15/21
BONUS 2😊
iOS 13 introduce new class called RelativeDateTimeFormatter(). This formatter gives chat date formatter for the given date string or date. Below is the little example for that.
func chatDateFormat() -> String {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
return formatter.localizedString(for: date1!, relativeTo: Date())
}
output
calling timeAgoDisplay()
Result: 1 week ago
Enjoy each and every line of coding. Hope this content will gives you a little bit clarity in how to use date formatters. Feel free to add comments here.