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

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

    Create Static Class Method:

    + (NSString *)stringForTimeIntervalSinceCreated:(NSDate *)dateTime serverTime:(NSDate *)serverDateTime{
        NSInteger MinInterval;
        NSInteger HourInterval;
        NSInteger DayInterval;
        NSInteger DayModules;   
    
        NSInteger interval = abs((NSInteger)[dateTime timeIntervalSinceDate:serverDateTime]);
        if(interval >= 86400)    
        {
            DayInterval  = interval/86400;
            DayModules = interval%86400;
            if(DayModules!=0)
            {
                if(DayModules>=3600){
                    //HourInterval=DayModules/3600;
                    return [NSString stringWithFormat:@"%i days", DayInterval];
                }
                else {
                    if(DayModules>=60){
                        //MinInterval=DayModules/60;
                        return [NSString stringWithFormat:@"%i days", DayInterval];
                    }
                    else {
                        return [NSString stringWithFormat:@"%i days", DayInterval];
                    }
                }
            }
            else 
            {
            return [NSString stringWithFormat:@"%i days", DayInterval];
            }
    
        }
    
        else{
    
            if(interval>=3600)
            {
    
                HourInterval= interval/3600;
                return [NSString stringWithFormat:@"%i hours", HourInterval];
    
            }
    
            else if(interval>=60){
    
                MinInterval = interval/60;
    
                return [NSString stringWithFormat:@"%i minutes", MinInterval];
            }
            else{
                return [NSString stringWithFormat:@"%i Sec", interval];
            }
    
        }
    
    }
    

提交回复
热议问题