Quick display of time back from date (NSDate)
In a cell, I want to display the time back from NSDate on the Parse server. Here is the code, but it doesn't work. Nothing changes and the data is not analyzed.
if let createdat = (object?["createdAt"] as? String){
let pastDate = Date(timeIntervalSinceNow: TimeInterval(createdat)!)
cell.TimeAgo.text = pastDate.timeAgoDisplay()
}
extension Date {
func timeAgoDisplay() -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))
let minute = 60
let hour = 60 * minute
let day = 24 * hour
let week = 7 * day
if secondsAgo < minute {
return "\(secondsAgo) sec ago"
} else if secondsAgo < hour {
return "\(secondsAgo / minute) min ago"
} else if secondsAgo < day {
return "\(secondsAgo / hour) hrs ago"
} else if secondsAgo < week {
return "\(secondsAgo / day) days ago"
}
return "\(secondsAgo / week) weeks ago"
}
}
source to share
If you just want the Time Ago extension for Date to move to the bottom of the answer :)
I'll show you an example to get a few seconds ago and after I show that your extension has been updated.
Note: you can directly use the date from Pase if you like:
if let pastDate = (object?["createdAt"] as? Date){
cell.TimeAgo.text = pastDate.timeAgoDisplay()
}
An example how to get a few seconds ago using Swift 3 or Swift 4 :
First: to get the number of seconds ago, we need to check if we have one minute or less to get the current date minus the minute, which you can write:
let minuteAgo = calendar.date(byAdding: .minute, value: -1, to: Date())!
Second: compare 2 dates! (In the case of your extension, we replace yourDate ourselves) and get the difference between the two dates.
if (minuteAgo < yourDate) {
let diff = Calendar.current.dateComponents([.second], from: yourDate, to: Date()).second ?? 0
print("\(diff) sec ago")
}
That's all, now you can print the time back!
So your extension looks like this: (This is a simple extension to get the time back)
extension Date {
func timeAgoDisplay() -> String {
let calendar = Calendar.current
let minuteAgo = calendar.date(byAdding: .minute, value: -1, to: Date())!
let hourAgo = calendar.date(byAdding: .hour, value: -1, to: Date())!
let dayAgo = calendar.date(byAdding: .day, value: -1, to: Date())!
let weekAgo = calendar.date(byAdding: .day, value: -7, to: Date())!
if minuteAgo < self {
let diff = Calendar.current.dateComponents([.second], from: self, to: Date()).second ?? 0
return "\(diff) sec ago"
} else if hourAgo < self {
let diff = Calendar.current.dateComponents([.minute], from: self, to: Date()).minute ?? 0
return "\(diff) min ago"
} else if dayAgo < self {
let diff = Calendar.current.dateComponents([.hour], from: self, to: Date()).hour ?? 0
return "\(diff) hrs ago"
} else if weekAgo < self {
let diff = Calendar.current.dateComponents([.day], from: self, to: Date()).day ?? 0
return "\(diff) days ago"
}
let diff = Calendar.current.dateComponents([.weekOfYear], from: self, to: Date()).weekOfYear ?? 0
return "\(diff) weeks ago"
}
}
To use it, it's very simple:
var now = Date()
now.timeAgoDisplay()
source to share
The transmission time as a string in the format, for example 2019-02-25 10:20:21. Pass dateformat in dateFormat variable
let dateFormat = "yyyy-MM-dd HH:mm:ss"
func timeInterval(timeAgo:String) -> String
{
let df = DateFormatter()
df.dateFormat = dateFormat
let dateWithTime = df.date(from: timeAgo)
let interval = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: dateWithTime!, 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 if let hour = interval.hour, hour > 0 {
return hour == 1 ? "\(hour)" + " " + "hour ago" : "\(hour)" + " " + "hours ago"
}else if let minute = interval.minute, minute > 0 {
return minute == 1 ? "\(minute)" + " " + "minute ago" : "\(minute)" + " " + "minutes ago"
}else if let second = interval.second, second > 0 {
return second == 1 ? "\(second)" + " " + "second ago" : "\(second)" + " " + "seconds ago"
} else {
return "a moment ago"
}
}
source to share
let now = Date()
let pastDate = Date(timeIntervalSinceNow: -60 * 60 * 24)
enum DisplayTime {
case short
case long
var seconds: String {
switch self {
case .short: return "s"
case .long: return "seconds"
}
}
var minutes: String {
switch self {
case .short: return "m"
case .long: return "minutes"
}
}
var hours: String {
switch self {
case .short: return "h"
case .long: return "hours"
}
}
var days: String {
switch self {
case .short: return "d"
case .long: return "days"
}
}
var weeks: String {
switch self {
case .short: return "w"
case .long: return "weeks"
}
}
}
extension Date {
func timeAgoDisplay(_ display: DisplayTime) -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))
let minute = 60
let hour = 60 * minute
let day = 24 * hour
let week = 7 * day
switch secondsAgo {
case let seconds where seconds < minute : return "\(secondsAgo) \(display.seconds) ago"
case let seconds where seconds < hour: return "\(secondsAgo / minute) \(display.minutes) ago"
case let seconds where seconds < day: return "\(secondsAgo / hour) \(display.hours) ago"
case let seconds where seconds < week: return "\(secondsAgo / day) \(display.days) ago"
default: "\(secondsAgo / week) \(display.weeks) ago"
}
return "\(secondsAgo / week) \(display.weeks) ago"
}
}
pastDate.timeAgoDisplay(.short)
source to share
Finally got a simple solution for me in swift4.2
let start = //Enter Start Date here....
let end = Date()
let formatter = DateComponentsFormatter()
formatter.maximumUnitCount = 2
formatter.unitsStyle = .full
formatter.allowedUnits = [.year, .month, .day]
let timeDifference = form.string(from: start, to: end)
print(timeDifference)
if timeDifference == "0 days"{
print("Today")
}
else if timeDifference == "1 days"{
print("\(timeDifference!)day ago")
}
else{
print("\(timeDifference!)days ago")
}
source to share