- (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;
}