How can I format a date in Objective-C similar to the jquery.timeago library?

前端 未结 9 1990
说谎
说谎 2020-12-29 05:57

I have a feed of items displayed in table cells, part of which is a date / timestamp in the past.

In Objective-C, how can I accomplish formatting them in the same m

9条回答
  •  执念已碎
    2020-12-29 06:36

    https://stackoverflow.com/a/8268903/1915957

    ↑ convert swift code.

    class func stringForTimeIntervalSinceCreated(dateTime :NSDate, serverTime serverDateTime:NSDate) -> String {
        var MinInterval  :Int = 0
        var HourInterval :Int = 0
        var DayInterval  :Int = 0
        var DayModules   :Int = 0
    
        let interval = abs(Int(dateTime.timeIntervalSinceDate(serverDateTime)))
        if (interval >= 86400) {
            DayInterval = interval/86400
            DayModules = interval%86400
            if (DayModules != 0) {
                if (DayModules>=3600) {
                    //HourInterval=DayModules/3600;
                    return String(DayInterval) + " days"
                } else {
                    if (DayModules >= 60) {
                        //MinInterval=DayModules/60;
                        return String(DayInterval) + " days"
                    } else {
                        return String(DayInterval) + " days"
                    }
                }
            } else {
                return String(DayInterval) + " days"
            }
        } else {
            if (interval >= 3600) {
                HourInterval = interval/3600
                return String(HourInterval) + " hours"
            } else if (interval >= 60) {
                MinInterval = interval/60
                return String(MinInterval) + " minutes"
            } else {
                return String(interval) + " Sec"
            }
        }
    }
    

提交回复
热议问题