Swift转换时间不限

时间:2015-12-24 20:03:37

标签: swift parse-platform nsdate

我正在尝试不断转换,我想做的是:

从1到15秒,它会说“刚才”

从60分钟到119分钟,它会说“一小时前”

从24小时到47小时,它会说“昨天”

从7天到7天和23小时,它会说“一周前”

我不确定我的计数如果我错了,请随时为我解决

这是代码

    extension NSDate {

    struct Date {
        static let timeFormatter: NSDateFormatter = {
            let df = NSDateFormatter()
            df.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
            df.dateFormat = "h:mm a"
            return df
            }()
    }
    var time: String { return Date.timeFormatter.stringFromDate(self) }

    var year:    Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Year,   fromDate: self) }
    var month:   Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Month,  fromDate: self) }
    var day:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Day,    fromDate: self) }
    var hour:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Hour,    fromDate: self) }
    var minute:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Minute,    fromDate: self) }
    var second:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Second,    fromDate: self) }


    struct DateComponents {
        static let formatter: NSDateComponentsFormatter = {
            let dcFormatter = NSDateComponentsFormatter()
            dcFormatter.calendar = NSCalendar(identifier: NSCalendarIdentifierISO8601)!
            dcFormatter.unitsStyle = .Full
            dcFormatter.maximumUnitCount = 1
            dcFormatter.zeroFormattingBehavior = .Default
            dcFormatter.allowsFractionalUnits = false
            dcFormatter.allowedUnits = [.Year, .Month, .Weekday, .Day, .Hour, .Minute, .Second]
            return dcFormatter
            }()
    }

    var elapsedTime: String {


        if isecond(NSDate()) {
            return "Just now"

        }

        if ishour(NSDate()) {
           return "an hour ago"

        }
        if isyesterday(NSDate()) {
            return "Yesterday"
        }
        if isweek(NSDate()) {

            return "a week ago"
        }




        return (DateComponents.formatter.stringFromTimeInterval(NSDate().timeIntervalSinceDate(self)) ?? "") + " ago"
    }
    func isyesterday(date: NSDate) -> Bool {
        return  NSCalendar.autoupdatingCurrentCalendar().isDate(self, inSameDayAsDate: date.yesterday)
    }
    var yesterday: NSDate {
        return  NSCalendar.autoupdatingCurrentCalendar().dateWithEra(1, year: year, month: month, day: day-1, hour: 0, minute: 0, second: 0, nanosecond: 0)!
    }



    func isweek(date: NSDate) -> Bool {
        return  NSCalendar.autoupdatingCurrentCalendar().isDate(self, inSameDayAsDate: date.weekago)
    }
    var weekago: NSDate {
        return  NSCalendar.autoupdatingCurrentCalendar().dateWithEra(1, year: year, month: month, day: day-7, hour: 0, minute: 0, second: 0, nanosecond: 0)!
    }

    func isecond(date: NSDate) -> Bool {
        return  NSCalendar.autoupdatingCurrentCalendar().isDate(self, inSameDayAsDate: date.secondsago)
    }
    var secondsago: NSDate {
        return  NSCalendar.autoupdatingCurrentCalendar().dateWithEra(1, year: year, month: month, day: 0, hour: 0, minute: 0, second: second-15, nanosecond: 0)!
    }

    func ishour(date: NSDate) -> Bool {
        return  NSCalendar.autoupdatingCurrentCalendar().isDate(self, inSameDayAsDate: date.hourago)
    }
    var hourago: NSDate {
        return  NSCalendar.autoupdatingCurrentCalendar().dateWithEra(1, year: year, month: month, day: 0, hour: hour-1, minute: minute-1, second: 0, nanosecond: 0)!
    }
}

这是我在我的应用程序中得到的: enter image description here

引用的是“一小时前”,“刚才”等,没有显示,只有“昨天”,我不确定它是否只有7天到23小时!

这是我的第一个问题链接:Swift How to convert Parse createdAt time to time ago?

请帮我修改代码, 感谢@Leo Dabus的代码。

13 个答案:

答案 0 :(得分:23)

我只是更新了Truongky对 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"

    }

}
}

如果您更喜欢可本地化的响应,而不仅仅是英语,则此代码将执行此工作

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!
}

答案 1 :(得分:12)

根据需要更改文字。

extension NSDate {

    func getElapsedInterval() -> String {

        var interval = NSCalendar.currentCalendar().components(.Year, fromDate: self, toDate: NSDate(), options: []).year

        if interval > 0 {
            return interval == 1 ? "\(interval)" + " " + "year" :
            "\(interval)" + " " + "years"
        }

        interval = NSCalendar.currentCalendar().components(.Month, fromDate: self, toDate: NSDate(), options: []).month
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " " + "month" :
            "\(interval)" + " " + "months"
        }

        interval = NSCalendar.currentCalendar().components(.Day, fromDate: self, toDate: NSDate(), options: []).day
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " " + "day" :
            "\(interval)" + " " + "days"
        }

        interval = NSCalendar.currentCalendar().components(.Hour, fromDate: self, toDate: NSDate(), options: []).hour
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " " + "hour" :
            "\(interval)" + " " + "hours"
        }

        interval = NSCalendar.currentCalendar().components(.Minute, fromDate: self, toDate: NSDate(), options: []).minute
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " " + "minute" :
            "\(interval)" + " " + "minutes"
        }

        return "a moment ago"
    }
}

答案 2 :(得分:7)

Swift 4+版本

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"
    }
}

用法

yourDate.timeAgoSinceDate()

答案 3 :(得分:6)

truongky's code的Swift 3版本:

extension Date {

    var timeAgoSinceNow: String {
        return getTimeAgoSinceNow()
    }

    private func getTimeAgoSinceNow() -> String {

        var interval = Calendar.current.dateComponents([.year], from: self, to: Date()).year!
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " year" : "\(interval)" + " years"
        }

        interval = Calendar.current.dateComponents([.month], from: self, to: Date()).month!
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " month" : "\(interval)" + " months"
        }

        interval = Calendar.current.dateComponents([.day], from: self, to: Date()).day!
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " day" : "\(interval)" + " days"
        }

        interval = Calendar.current.dateComponents([.hour], from: self, to: Date()).hour!
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " hour" : "\(interval)" + " hours"
        }

        interval = Calendar.current.dateComponents([.minute], from: self, to: Date()).minute!
        if interval > 0 {
            return interval == 1 ? "\(interval)" + " minute" : "\(interval)" + " minutes"
        }

        return "a moment ago"
    }
}

答案 4 :(得分:1)

使用日期代替NSDate:Answer taking from:

func timeAgoSinceDate(_ date:Date, numericDates:Bool = false) -> String {
    let calendar = NSCalendar.current
    let unitFlags: Set<Calendar.Component> = [.minute, .hour, .day, .weekOfYear, .month, .year, .second]
    let now = Date()
    let earliest = now < date ? now : date
    let latest = (earliest == now) ? date : now
    let components = calendar.dateComponents(unitFlags, from: earliest,  to: latest)

    if (components.year! >= 2) {
        return "\(components.year!) years ago"
    } else if (components.year! >= 1){
        if (numericDates){
            return "1 year ago"
        } else {
            return "Last year"
        }
    } else if (components.month! >= 2) {
        return "\(components.month!) months ago"
    } else if (components.month! >= 1){
        if (numericDates){
            return "1 month ago"
        } else {
            return "Last month"
        }
    } else if (components.weekOfYear! >= 2) {
        return "\(components.weekOfYear!) weeks ago"
    } else if (components.weekOfYear! >= 1){
        if (numericDates){
            return "1 week ago"
        } else {
            return "Last week"
        }
    } else if (components.day! >= 2) {
        return "\(components.day!) days ago"
    } else if (components.day! >= 1){
        if (numericDates){
            return "1 day ago"
        } else {
            return "Yesterday"
        }
    } else if (components.hour! >= 2) {
        return "\(components.hour!) hours ago"
    } else if (components.hour! >= 1){
        if (numericDates){
            return "1 hour ago"
        } else {
            return "An hour ago"
        }
    } else if (components.minute! >= 2) {
        return "\(components.minute!) minutes ago"
    } else if (components.minute! >= 1){
        if (numericDates){
            return "1 minute ago"
        } else {
            return "A minute ago"
        }
    } else if (components.second! >= 3) {
        return "\(components.second!) seconds ago"
    } else {
        return "Just now"
    }

}

如何使用

var createdDate : String = "123232323" //time stamp
var createdDataDisplayString: String {
    let date = Date(timeIntervalSince1970: Double(self.createdDate)!).timeAgoSinceDate(numericDates: true)
    return "\(date)"
}
print(self.createdDataDisplayString)

答案 5 :(得分:1)

迅速4.1。在函数参数中传递日期给您,函数将返回过去的时间。

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
    }
}

答案 6 :(得分:1)

您需要将此日期扩展名添加到您的帮助器类或任何快速文件中。 Swift 4.2 。如果您想在 swift 3 中取消注释,请注释下一行。

并按如下所示调用方法:

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

您的日期是要更改的日期

////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 ""

}

}

答案 7 :(得分:1)

在Swift 5上,使用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"

设置dateTimeStyle获取本地化的声明语句,例如-

formatter.dateTimeStyle = .named 
formatter.localizedString(from: DateComponents(day: -1)) // "yesterday"

答案 8 :(得分:0)

我的需求更简单,“现在”或“ x以前”

从iOS 8开始,您可以使用(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 ?? ""
}

例如

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())

现在
10m前
1小时前
1天前
1mo前
1年前

答案 9 :(得分:0)

这是您可以复制粘贴的解决方案

 func timeAgoSinceDate(_ date:Date, numericDates:Bool = false) -> String? {
    let calendar = NSCalendar.current
    let unitFlags: Set<Calendar.Component> = [.minute, .hour, .day, .weekOfYear, .month, .year, .second]
    let now = Date()
    let earliest = now < date ? now : date
    let latest = (earliest == now) ? date : now
    let components = calendar.dateComponents(unitFlags, from: earliest,  to: latest)

    if (components.year! >= 2) {
        return "\(components.year!)yr"
    } else if (components.year! >= 1){
        if (numericDates){
            return "1yr"
        } else {
            return "Last year"
        }
    } else if (components.month! >= 2) {
        return "\(components.month!)mo"
    } else if (components.month! >= 1){
        if (numericDates){
            return "1 mo"
        } else {
            return "Last mo"
        }
    } else if (components.weekOfYear! >= 2) {
        return "\(components.weekOfYear!) weeks"
    } else if (components.weekOfYear! >= 1){
        if (numericDates){
            return "1 week"
        } else {
            return "Last week"
        }
    } else if (components.day! >= 2) {
        return "\(components.day!) d"
    } else if (components.day! >= 1){
        if (numericDates){
            return "1 d"
        } else {
            return "1 d"
        }
    } else if (components.hour! >= 2) {
        return "\(components.hour!) hrs"
    } else if (components.hour! >= 1){
        if (numericDates){
            return "1 hr"
        } else {
            return "1 hr"
        }
    } else if (components.minute! >= 2) {
        return "\(components.minute!) m"
    } else if (components.minute! >= 1){
        if (numericDates){
            return "1 m"
        } else {
            return "2 m"
        }
    } else if (components.second! >= 3) {
        return "\(components.second!)s"
    } else {
        return "now"
    }

}

拨打电话,只需使用

    // the date you are getting information about 
let dateToReference = Date()
            if let timeAgo = timeAgoSinceDate(dateToReference) {
                //will be a String value
                print(timeAgo)

            } else {
                //just in case something goes wrong
                print("recently")

            }

答案 10 :(得分:0)

truongky's code:的Swift 4版本

并且现在不为TimeEnd添加可选。

extension Date {

    func getElapsedInterval(to end: Date = Date()) -> String {

        if let interval = Calendar.current.dateComponents([Calendar.Component.year], from: self, to: end).day {
            if interval > 0 {
                return "\(interval) year\(interval == 1 ? "":"s")"
            }
        }

        if let interval = Calendar.current.dateComponents([Calendar.Component.month], from: self, to: end).month {
            if interval > 0 {
                return "\(interval) month\(interval == 1 ? "":"s")"
            }
        }

        if let interval = Calendar.current.dateComponents([Calendar.Component.weekOfMonth], from: self, to: end).weekOfMonth {
            if interval > 0 {
                return "\(interval) week\(interval == 1 ? "":"s")"
            }
        }

        if let interval = Calendar.current.dateComponents([Calendar.Component.day], from: self, to: end).day {
            if interval > 0 {
                return "\(interval) day\(interval == 1 ? "":"s")"
            }
        }

        if let interval = Calendar.current.dateComponents([Calendar.Component.hour], from: self, to: end).hour {
            if interval > 0 {
                return "\(interval) hour\(interval == 1 ? "":"s")"
            }
        }

        if let interval = Calendar.current.dateComponents([Calendar.Component.minute], from: self, to: end).minute {
            if interval > 0 {
                return "\(interval) minute\(interval == 1 ? "":"s")"
            }
        }

        return "Just now."
    }

}

答案 11 :(得分:0)

对于 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"
    }
}

使用方法

oldDate.timeAgoSinceDate()

快乐编码:)

答案 12 :(得分:0)

类似于使用更清晰的字符串处理的已接受答案:

extension Date
{
    func getElapsedInterval() -> String
    {
        let interval = Calendar.current.dateComponents([.year, .month, .day], from: self, to: Date())
        
        if let yearsPassed = interval.year,
            yearsPassed > 0
        {
            return "\(yearsPassed) year\(yearsPassed == 1 ? "" : "s") ago"
        }
        else if let monthsPassed = interval.month,
            monthsPassed > 0
        {
            return "\(monthsPassed) month\(monthsPassed == 1 ? "" : "s") ago"
        }
        else if let daysPassed = interval.day,
            daysPassed > 0
        {
            return "\(daysPassed) day\(daysPassed == 1 ? "" : "s") ago"
        }
        
        return "a moment ago"
    }
}