Swift iOS doesRelativeDateFormatting have differen

2019-01-18 12:00发布

问题:

I have a number of dates that I am trying to represent using a relative date such as "Today, Yesterday, 1 week ago, 1 month ago" etc...

This is the Swift code I am using:

let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
dateFormatter.doesRelativeDateFormatting = true
let uploadDate = dateFormatter.stringFromDate(date)

Only "Today" and "Yesterday" are represented and the rest of the dates appear like the below: Nov 24, 2014 Nov 17, 2014 etc...

Is there another way to represent all the dates using relative dating instead of the actual dates?

回答1:

edit/Update: Xcode 8.3.2 • Swift 3.1

It is really easy if you use extensions and Calendar methods to help you with your calendrical calculations:

extension Date {
    var yearsFromNow:   Int { return Calendar.current.dateComponents([.year],       from: self, to: Date()).year        ?? 0 }
    var monthsFromNow:  Int { return Calendar.current.dateComponents([.month],      from: self, to: Date()).month       ?? 0 }
    var weeksFromNow:   Int { return Calendar.current.dateComponents([.weekOfYear], from: self, to: Date()).weekOfYear  ?? 0 }
    var daysFromNow:    Int { return Calendar.current.dateComponents([.day],        from: self, to: Date()).day         ?? 0 }
    var hoursFromNow:   Int { return Calendar.current.dateComponents([.hour],       from: self, to: Date()).hour        ?? 0 }
    var minutesFromNow: Int { return Calendar.current.dateComponents([.minute],     from: self, to: Date()).minute      ?? 0 }
    var secondsFromNow: Int { return Calendar.current.dateComponents([.second],     from: self, to: Date()).second      ?? 0 }
    var relativeTime: String {
        if yearsFromNow   > 0 { return "\(yearsFromNow) year"    + (yearsFromNow    > 1 ? "s" : "") + " ago" }
        if monthsFromNow  > 0 { return "\(monthsFromNow) month"  + (monthsFromNow   > 1 ? "s" : "") + " ago" }
        if weeksFromNow   > 0 { return "\(weeksFromNow) week"    + (weeksFromNow    > 1 ? "s" : "") + " ago" }
        if daysFromNow    > 0 { return daysFromNow == 1 ? "Yesterday" : "\(daysFromNow) days ago" }
        if hoursFromNow   > 0 { return "\(hoursFromNow) hour"     + (hoursFromNow   > 1 ? "s" : "") + " ago" }
        if minutesFromNow > 0 { return "\(minutesFromNow) minute" + (minutesFromNow > 1 ? "s" : "") + " ago" }
        if secondsFromNow > 0 { return secondsFromNow < 15 ? "Just now"
                            : "\(secondsFromNow) second" + (secondsFromNow > 1 ? "s" : "") + " ago" }
        return ""
    }
}

Testing

let calendar =  Calendar.current

let date1 = DateComponents(calendar: calendar, year: 2016, month:  3, day: 14, hour: 22, minute: 39).date!
let date2 = DateComponents(calendar: calendar, year: 2017, month:  5, day: 18, hour: 22, minute: 39).date!
let date3 = DateComponents(calendar: calendar, year: 2017, month: 6, day:  2, hour: 12, minute: 38).date!
let date4 = DateComponents(calendar: calendar, year: 2017, month: 6, day:  3, hour: 14, minute: 45).date!
let date5 = DateComponents(calendar: calendar, year: 2017, month: 6, day:  3, hour: 15, minute:  18).date!

let timeOffset1 = date1.relativeTime // "1 year ago"
let timeOffset2 = date2.relativeTime // "2 weeks ago"
let timeOffset3 = date3.relativeTime // "Yesterday"
let timeOffset4 = date4.relativeTime // "33 minutes ago"
let timeOffset5 = date5.relativeTime // "Just now"


回答2:

You should have a look at Mattt Thompsons Blog post about NSDateComponentsFormatter

http://nshipster.com/nsdatecomponents/ and http://nshipster.com/nsformatter/

let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full

let components = NSDateComponents()
components.day = 1
components.hour = 2

let string = formatter.stringFromDateComponents(components)
// 1 day, 2 hours

Example (by Mattt Thompson)



回答3:

var relativeTime: String {
    // don't forget spacing on these constants.
    let futurePrefix = "" // " in" is a viable choice here...
    let pastSuffix = " ago" // " ago" is a viable choice here...
    let now = NSDate()
    for (method, unitName) in [
        (now.years, "year"),
        (now.months, "month"),
        (now.weeks, "week"),
        (now.days, "day"),
        (now.hours, "hour"),
        (now.minutes, "minute"),
        (now.seconds, "second")
    ] {
        let qty = method(self)
        let absQty = abs(method(self))
        if absQty > 0 {
            return (qty < 0 ? futurePrefix : "") +
                String(absQty) + " " +
                unitName +
                (absQty == 1 ? "": "s") +
                (qty > 0 ? pastSuffix : "")
        }
    }
    return ""
}

An alternative implementation of Leo Dabus's implementation of relativeTime that does both future and past and is more succinct. (Swift2.3)