time ago

PHOTO EMBED

Thu Feb 23 2023 19:17:31 GMT+0000 (Coordinated Universal Time)

Saved by @hasnat #ios #swift #time #date #chat

    func timeAgoDisplay(previousTime : Int) -> String {
        
        let cTime = Int(NSDate().timeIntervalSince1970)
        let pTime = previousTime/1000
        
        let secondsAgo = cTime - pTime
        
        let minute = 60
        let hour = 60 * minute
        let day = 24 * hour
        let week = 7 * day
        let month = day * 30
        let year = month * 12
        
        
        
        if secondsAgo < minute {
            var txt = ""
            if secondsAgo == 1 {
                txt = "second ago"
            } else {
                txt = "seconds ago"
            }
            return "\(secondsAgo) \(txt)"
        } else if secondsAgo < hour {
            var txt = ""
            let minutes = secondsAgo / minute
            if minutes == 1 {
                txt = "minute ago"
            } else {
                txt = "minutes ago"
            }
            return "\(minutes) \(txt)"
        } else if secondsAgo < day {
            var txt = ""
            let hours = secondsAgo / hour
            if hours == 1 {
                txt = "hour ago"
            } else {
                txt = "hours ago"
            }
            return "\(hours) \(txt)"
        } else if secondsAgo < week {
            var txt = ""
            let days = secondsAgo / day
            if days == 1 {
                txt = "day ago"
            } else {
                txt = "days ago"
            }
            return "\(days) \(txt)"
        } else if secondsAgo < month {
            var txt = ""
            let weeks = secondsAgo / week
            if weeks == 1 {
                txt = "week ago"
            } else {
                txt = "weeks ago"
            }
            return "\(weeks) \(txt)"
        } else if secondsAgo < year {
            var txt = ""
            let months = secondsAgo / month
            if months == 1 {
                txt = "month ago"
            } else {
                txt = "months ago"
            }
            return "\(months) \(txt)"
        } else {
            var txt = ""
            let years = secondsAgo / year
            if years == 1 {
                txt = "year ago"
            } else {
                txt = "years ago"
            }
            return "\(years) \(txt)"
        }
    }
content_copyCOPY