-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeAgoAlgorithm.swift
41 lines (30 loc) · 1.02 KB
/
TimeAgoAlgorithm.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import UIKit
extension Date {
func timeAgoDisplay() -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))
let minute = 60
let hour = minute * 60
let day = hour * 24
let week = day * 7
let month = week * 4
let year = month * 12
if secondsAgo < minute {
return "\(secondsAgo) seconds ago"
}else if secondsAgo < hour {
return "\(secondsAgo / minute) minutes ago"
}else if secondsAgo < day {
return "\(secondsAgo / hour) hours ago"
}else if secondsAgo < week {
return "\(secondsAgo / day) days ago"
}else if secondsAgo < month {
return "\(secondsAgo / week) weeks ago"
}else if secondsAgo < year {
return "\(secondsAgo / month) months ago"
}else {
return "\(secondsAgo / year) years ago"
}
}
}
let now = Date()
let pastDate = Date(timeIntervalSinceNow: -60)
pastDate.timeAgoDisplay()