iOS-OC根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)

- (NSString*)distanceTimeWithBeforeTime:(double)beTime

{

NSTimeIntervalnow = [[NSDatedate]timeIntervalSince1970];

doubledistanceTime = now - beTime;

NSString* distanceStr;

NSDate* beDate = [NSDatedateWithTimeIntervalSince1970:beTime];

NSDateFormatter* df = [[NSDateFormatteralloc]init];

[dfsetDateFormat:@"HH:mm"];

NSString* timeStr = [dfstringFromDate:beDate];

[dfsetDateFormat:@"dd"];

NSString* nowDay = [dfstringFromDate:[NSDatedate]];

NSString* lastDay = [dfstringFromDate:beDate];

if(distanceTime <60) {//小于一分钟

distanceStr =@"刚刚";

}

elseif(distanceTime <60*60) {//时间小于一个小时

distanceStr = [NSStringstringWithFormat:@"%ld分钟前",(long)distanceTime/60];

}

elseif(distanceTime <24*60*60&& [nowDayintegerValue] == [lastDayintegerValue]){//时间小于一天

distanceStr = [NSStringstringWithFormat:@"今天%@",timeStr];

}

elseif(distanceTime<24*60*60*2&& [nowDayintegerValue] != [lastDayintegerValue]){

if([nowDayintegerValue] - [lastDayintegerValue] ==1|| ([lastDayintegerValue] - [nowDayintegerValue] >10&& [nowDayintegerValue] ==1)) {

distanceStr = [NSStringstringWithFormat:@"昨天%@",timeStr];

}

else{

[dfsetDateFormat:@"MM-dd HH:mm"];

distanceStr = [dfstringFromDate:beDate];

}

}

elseif(distanceTime <24*60*60*365){

[dfsetDateFormat:@"MM-dd HH:mm"];

distanceStr = [dfstringFromDate:beDate];

}

else{

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

distanceStr = [dfstringFromDate:beDate];

}

returndistanceStr;

}

你可能感兴趣的:(iOS-OC根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天))