iOS

显示发送时间(几分钟前,几小时前,几天前)

2018-10-31  本文已影响0人  三浦蒼介

- (NSString*) compareCurrentTime:(NSString*)str

{

    //把字符串转为NSdate

    //    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

    //    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    //    currDate = [formatter stringFromDate:[NSDate date]];

    long long time = [str longLongValue];

    NSDate *d = [[NSDate alloc]initWithTimeIntervalSince1970:time/1000.0];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm"];

    NSString*currentDateString = [dateFormatterstringFromDate:d];

    NSDate*currentDate = [NSDatedate];

    NSTimeIntervaltimeInterval = [currentDatetimeIntervalSinceDate:d];

    longtemp =0;

    NSString*result;

    if(timeInterval/60<1)

    {

        result = [NSString stringWithFormat:@"刚刚"];

    }

    elseif((temp = timeInterval/60) <60){

        result = [NSStringstringWithFormat:@"%ld分钟前",temp];

    }

    elseif((temp = temp/60) <24){

        result = [NSStringstringWithFormat:@"%ld小时前",temp];

    }

    elseif((temp = temp/24) <30){

      result = [NSString stringWithFormat:@"%ld天前",temp];

    }

    elseif((temp = temp/30) <12){

        result = [NSString stringWithFormat:@"%ld月前",temp];    }

    else{

        temp = temp/12;

        result = [NSString stringWithFormat:@"%ld年前",temp];

    }

    return  result;

}

上一篇下一篇

猜你喜欢

热点阅读