Date Extension-iOS(Swift)

Lucky Mehndiratta
2 min readMay 18, 2022

Sometimes we need to modify the date format as we got it from the server to display on the UI or we need to send it to the server as we got it from the date picker. Below is some useful method for Date Formatting.

extension Date {

func string(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}

func getTime()-> String {
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = DateFormatter.Style.short
dateFormatter.timeZone = TimeZone.current
let time = dateFormatter.string(from:self)
return time
}

func timeAgoSinceDate(numericDates:Bool) -> String {

let calendar = Calendar.current
let now = Date()
let earliest = (now as NSDate).earlierDate(self)
let latest = (earliest == now) ? self : now
let components:DateComponents = (calendar as NSCalendar).components([NSCalendar.Unit.minute , NSCalendar.Unit.hour , NSCalendar.Unit.day , NSCalendar.Unit.weekOfYear , NSCalendar.Unit.month , NSCalendar.Unit.year , NSCalendar.Unit.second], from: earliest, to: latest, options: NSCalendar.Options())
if (components.year! >= 2) {
return “\(components.year!) years ago”
} else if (components.year! >= 1){
if (numericDates){
return “1 year ago”
} else {
return “Last year”
}
} else if (components.month! >= 2) {
return “\(components.month!) months ago”
} else if (components.month! >= 1){
if (numericDates){
return “1 month ago”
} else {
return “Last month”
}
} else if (components.weekOfYear! >= 2) {
return “\(components.weekOfYear!) weeks ago”
} else if (components.weekOfYear! >= 1){
if (numericDates){
return “1 week ago”
} else {
return “Last week”
}
} else if (components.day! >= 2) {
return “\(components.day!) days ago”
} else if (components.day! >= 1){
if (numericDates){
return “1 day ago”
} else {
return “Yesterday”
}
} else if (components.hour! >= 2) {
return “\(components.hour!) hours ago”
} else if (components.hour! >= 1){
if (numericDates){
return “1 hour ago”
} else {
return “An hour ago”
}
} else if (components.minute! >= 2) {
return “\(components.minute!) minutes ago”
} else if (components.minute! >= 1){
if (numericDates){
return “1 minute ago”
} else {
return “A minute ago”
}
} else if (components.second! >= 3) {
return “\(components.second!) seconds ago”
} else {
return “Just now”
}
}

// Date().string(format: “EEEE, MMM d, yyyy”) // Saturday, Oct 21, 2017
// Date().string(format: “MM/dd/yyyy”) // 10/21/2017
// Date().string(format: “MM-dd-yyyy HH:mm”) // 10–21–2017 03:31
// Date().string(format: “MMM d, h:mm a”) // Oct 21, 3:31 AM
// Date().string(format: “MMMM yyyy”) // October 2017
// Date().string(format: “MMM d, yyyy”) // Oct 21, 2017
// Date().string(format: “E, d MMM yyyy HH:mm:ss Z”) // Sat, 21 Oct 2017 03:31:40 +0000
// Date().string(format: “yyyy-MM-dd’T’HH:mm:ssZ”) // 2017–10–21T03:31:40+0000
// Date().string(format: “dd.MM.yy”)
// Date().string(format: “EEEE, MMM d, yyyy”)

}

Happy Coding!!

--

--