Swift convert time to time ago
Swift 4+ Version
extension Date {
func timeAgoSinceDate() -> String {
// From Time
let fromDate = self
// To Time
let toDate = Date()
// Estimation
// Year
if let interval = Calendar.current.dateComponents([.year], from: fromDate, to: toDate).year, interval > 0 {
return interval == 1 ? "\(interval)" + " " + "year ago" : "\(interval)" + " " + "years ago"
}
// Month
if let interval = Calendar.current.dateComponents([.month], from: fromDate, to: toDate).month, interval > 0 {
return interval == 1 ? "\(interval)" + " " + "month ago" : "\(interval)" + " " + "months ago"
}
// Day
if let interval = Calendar.current.dateComponents([.day], from: fromDate, to: toDate).day, interval > 0 {
return interval == 1 ? "\(interval)" + " " + "day ago" : "\(interval)" + " " + "days ago"
}
// Hours
if let interval = Calendar.current.dateComponents([.hour], from: fromDate, to: toDate).hour, interval > 0 {
return interval == 1 ? "\(interval)" + " " + "hour ago" : "\(interval)" + " " + "hours ago"
}
// Minute
if let interval = Calendar.current.dateComponents([.minute], from: fromDate, to: toDate).minute, interval > 0 {
return interval == 1 ? "\(interval)" + " " + "minute ago" : "\(interval)" + " " + "minutes ago"
}
return "a moment ago"
}
}
Usage
yourDate.timeAgoSinceDate()
I'll just update the Truongky's answer for Swif 3:
extension Date {
func getElapsedInterval() -> String {
let interval = Calendar.current.dateComponents([.year, .month, .day], from: self, to: Date())
if let year = interval.year, year > 0 {
return year == 1 ? "\(year)" + " " + "year ago" :
"\(year)" + " " + "years ago"
} else if let month = interval.month, month > 0 {
return month == 1 ? "\(month)" + " " + "month ago" :
"\(month)" + " " + "months ago"
} else if let day = interval.day, day > 0 {
return day == 1 ? "\(day)" + " " + "day ago" :
"\(day)" + " " + "days ago"
} else {
return "a moment ago"
}
}
}
If you prefer a localizable response instead of only english this code will do the work
extension Date {
func getElapsedInterval() -> String {
var calendar = Calendar.current
calendar.locale = Locale(identifier: Bundle.main.preferredLocalizations[0])
// IF THE USER HAVE THE PHONE IN SPANISH BUT YOUR APP ONLY SUPPORTS I.E. ENGLISH AND GERMAN
// WE SHOULD CHANGE THE LOCALE OF THE FORMATTER TO THE PREFERRED ONE
// (IS THE LOCALE THAT THE USER IS SEEING THE APP), IF NOT, THIS ELAPSED TIME
// IS GOING TO APPEAR IN SPANISH
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.maximumUnitCount = 1
formatter.calendar = calendar
var dateString: String?
let interval = calendar.dateComponents([.year, .month, .weekOfYear, .day], from: self, to: Date())
if let year = interval.year, year > 0 {
formatter.allowedUnits = [.year] //2 years
} else if let month = interval.month, month > 0 {
formatter.allowedUnits = [.month] //1 month
} else if let week = interval.weekOfYear, week > 0 {
formatter.allowedUnits = [.weekOfMonth] //3 weeks
} else if let day = interval.day, day > 0 {
formatter.allowedUnits = [.day] // 6 days
} else {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: Bundle.main.preferredLocalizations[0]) //--> IF THE USER HAVE THE PHONE IN SPANISH BUT YOUR APP ONLY SUPPORTS I.E. ENGLISH AND GERMAN WE SHOULD CHANGE THE LOCALE OF THE FORMATTER TO THE PREFERRED ONE (IS THE LOCALE THAT THE USER IS SEEING THE APP), IF NOT, THIS ELAPSED TIME IS GOING TO APPEAR IN SPANISH
dateFormatter.dateStyle = .medium
dateFormatter.doesRelativeDateFormatting = true
dateString = dateFormatter.string(from: self) // IS GOING TO SHOW 'TODAY'
}
if dateString == nil {
dateString = formatter.string(from: self, to: Date())
}
return dateString!
}