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

前端 未结 9 1945
说谎
说谎 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:35

    You can try with NSDateFormater and write it your self.

    If value is under one minute then write just now, otherwise minutes, hours, days, etc.

    0 讨论(0)
  • 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"
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 06:39
    + (NSString *)changeTime:(NSString *)serverDateTime{
        NSInteger MinInterval;
        NSInteger HourInterval;
        NSInteger DayInterval;
        //NSInteger DayModules;
        NSTimeInterval _interval=[serverDateTime doubleValue];
        NSDate *Serverdate = [NSDate dateWithTimeIntervalSince1970:_interval];
        NSInteger interval = (long)((NSInteger)[[NSDate date] timeIntervalSinceDate:Serverdate]);
        if(interval >= 86400)
        {
            DayInterval  = interval/86400;// no. of days
            if (DayInterval > 14){
                return [NSString stringWithFormat:@"w+"];
            }
            if (DayInterval >= 7 && DayInterval <= 14){
                int diff =  (int)DayInterval / 7;
                return [NSString stringWithFormat:@"%iw",diff];
            }
            return [NSString stringWithFormat:@"%id",(int)DayInterval];
        }
        else{
    
            if(interval>=3600)
            {
    
                HourInterval= interval/3600;
                return [NSString stringWithFormat:@"%lih", (long)HourInterval];
    
            }
            else if(interval>=60){
    
                MinInterval = interval/60;
    
                return [NSString stringWithFormat:@"%lim", (long)MinInterval];
            }
            else{
                return [NSString stringWithFormat:@"%lis", (long)interval];
            }
            return @"now";
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题