Swift convert time to time ago

前端 未结 13 1204
一个人的身影
一个人的身影 2020-12-24 02:14

In Swift5, we have RelativeDateTimeFormatter

Prior to Swift5:


I\'m trying to convert time to time ago, what i wanna do is:

from 1

相关标签:
13条回答
  • 2020-12-24 02:30

    Swift 4.1. Pass you date in function param and function will return past time.

    func getPastTime(for date : Date) -> String {
    
        var secondsAgo = Int(Date().timeIntervalSince(date))
        if secondsAgo < 0 {
            secondsAgo = secondsAgo * (-1)
        }
    
        let minute = 60
        let hour = 60 * minute
        let day = 24 * hour
        let week = 7 * day
    
        if secondsAgo < minute  {
            if secondsAgo < 2{
                return "just now"
            }else{
                return "\(secondsAgo) secs ago"
            }
        } else if secondsAgo < hour {
            let min = secondsAgo/minute
            if min == 1{
                return "\(min) min ago"
            }else{
                return "\(min) mins ago"
            }
        } else if secondsAgo < day {
            let hr = secondsAgo/hour
            if hr == 1{
                return "\(hr) hr ago"
            } else {
                return "\(hr) hrs ago"
            }
        } else if secondsAgo < week {
            let day = secondsAgo/day
            if day == 1{
                return "\(day) day ago"
            }else{
                return "\(day) days ago"
            }
        } else {
            let formatter = DateFormatter()
            formatter.dateFormat = "MMM dd, hh:mm a"
            formatter.locale = Locale(identifier: "en_US")
            let strDate: String = formatter.string(from: date)
            return strDate
        }
    }
    
    0 讨论(0)
  • 2020-12-24 02:30

    You need to add this date extension in your helper class or any in swift file. Swift 4.2 . if you want in swift 3 just uncomment the commented line and comment the next line.

    And call the method as follow:

     let dateText = Date().offsetFrom(date: yourDate!)
    

    yourDate is date that to be change

    ////MARK: *************** Extension Date ***************
     extension Date{
    
    
    func convertToString(validDateFormatter:String) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = validDateFormatter //"dd MMM yyyy" //yyyy-mm-dd hh:mm
        //return dateFormatter.stringFromDate(self)
        return dateFormatter.string(from: self as Date)
    
    }
    
    func yearsFrom(date:Date) -> Int{
        //        return NSCalendar.currentCalendar.components(.Year, fromDate: date, toDate: self, options: []).year
        //        let calendar = NSCalendar.current
    
        //        let components = Calendar.current.dateComponents([.year], from: date, to: self as Date)
    
        return Calendar.current.dateComponents([.year], from: date, to: self).year!
    
    }
    func monthsFrom(date:Date) -> Int{
        //        return NSCalendar.currentCalendar.components(.Month, fromDate: date, toDate: self, options: []).month
        return Calendar.current.dateComponents([.month], from: date as Date, to: self).month!
    }
    func weeksFrom(date:Date) -> Int{
        //return NSCalendar.currentCalendar.components(.WeekOfYear, fromDate: date, toDate: self, options: []).weekOfYear
        return Calendar.current.dateComponents([.weekOfYear], from: date, to: self).weekOfYear!
    }
    func daysFrom(date:Date) -> Int{
        //        return NSCalendar.currentCalendar.components(.Day, fromDate: date, toDate: self, options: []).day
        return Calendar.current.dateComponents([.day], from: date, to: self).day!
    }
    func hoursFrom(date:Date) -> Int{
        //        return NSCalendar.currentCalendar.components(.Hour, fromDate: date, toDate: self, options: []).hour
        return Calendar.current.dateComponents([.hour], from: date, to: self).hour!
    }
    func minutesFrom(date:Date) -> Int{
        //        return NSCalendar.currentCalendar.components(.Minute, fromDate: date, toDate: self, options: []).minute
        return Calendar.current.dateComponents([.minute], from: date, to: self).minute!
    }
    func secondsFrom(date:Date) -> Int{
        //        return NSCalendar.currentCalendar.components(.Second, fromDate: date, toDate: self, options: []).second
        return Calendar.current.dateComponents([.second], from: date, to: self).second!
    }
    func offsetFrom(date:Date) -> String
    {
        if yearsFrom(date: date) > 0 {
            return "\(yearsFrom(date: date))y"
        }
        if monthsFrom(date: date) > 0 {
            return "\(monthsFrom(date: date))M"
        }
        if weeksFrom(date: date) > 0{
            return "\(weeksFrom(date: date))w"
        }
        if daysFrom(date: date) > 0 {
            return "\(daysFrom(date: date))d"
        }
        if hoursFrom(date: date) > 0 {
            return "\(hoursFrom(date: date))h"
        }
        if minutesFrom(date: date) > 0 {
            return "\(minutesFrom(date: date))m"
        }
        if secondsFrom(date: date) > 0 {
            return "\(secondsFrom(date: date))s"
        }
        return ""
    
    }
    
    }
    
    0 讨论(0)
  • 2020-12-24 02:31

    For swift 5.x

    extension Date {
    
        func timeAgoSinceDate() -> String {
    
            // From Time
            let fromDate = self
    
            // To Time
            let toDate = Date()
    
            // 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 "just now"
        }
    }
    

    How to use

    oldDate.timeAgoSinceDate()
    

    Happy Coding :)

    0 讨论(0)
  • 2020-12-24 02:34

    My needs are simpler, either "now" or "x ago"

    Since iOS 8 you can use (NS)DateComponentsFormatter

    func getElapsedInterval() -> String {
    
        let formatter = DateComponentsFormatter()
        formatter.allowedUnits = [.minute, .hour, .day, .month, .year]
        formatter.unitsStyle = .abbreviated
        formatter.zeroFormattingBehavior = .dropAll
        formatter.maximumUnitCount = 1
    
        var dateString: String?
    
        if self.timeIntervalSince(Date()) > -60*5 {
            dateString = NSLocalizedString("now", comment: "")
        } else {
            dateString = String.init(format: NSLocalizedString("%@ ago", comment: ""), locale: .current, formatter.string(from: self, to: Date())!)
        }
    
        return dateString ?? ""
    }
    

    e.g.

    var date = Date.init(timeIntervalSinceNow: -60)
    print(date.getElapsedInterval())
    date = Date.init(timeIntervalSinceNow: -60*10)
    print(date.getElapsedInterval())
    date = Date.init(timeIntervalSinceNow: -60*60)
    print(date.getElapsedInterval())
    date = Date.init(timeIntervalSinceNow: -60*60*24)
    print(date.getElapsedInterval())
    date = Date.init(timeIntervalSinceNow: -60*60*24*30)
    print(date.getElapsedInterval())
    date = Date.init(timeIntervalSinceNow: -60*60*24*31*12)
    print(date.getElapsedInterval())
    

    now
    10m ago
    1h ago
    1d ago
    1mo ago
    1y ago

    0 讨论(0)
  • 2020-12-24 02:38

    On Swift 5 use the RelativeDateTimeFormatter,

    let formatter = RelativeDateTimeFormatter()
    
    formatter.localizedString(from: DateComponents(day: -1)) // "1 day ago"
    formatter.localizedString(from: DateComponents(hour: 2)) // "in 2 hours"
    formatter.localizedString(from: DateComponents(minute: 45)) // "in 45 minutes"
    

    set the dateTimeStyle get localized deictic statements e.g. -

    formatter.dateTimeStyle = .named 
    formatter.localizedString(from: DateComponents(day: -1)) // "yesterday"
    
    0 讨论(0)
  • 2020-12-24 02:41

    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()
    
    0 讨论(0)
提交回复
热议问题