将时间戳转化为多长时间前
只接写代码
- (NSString*)distanceNowTime{
double nowTime = [[NSDate date] timeIntervalSince1970];
NSString *nowStr = [NSString stringWithFormat:@"%.lf",nowTime];
double nowTimebb = [nowStr doubleValue];
double time = [self doubleValue];//////注意这里的self是后台反回来的时间戳字符串
if (time > 999999999999) {
time = time/1000;
}
NSLog(@"==%.lf----%.lf",time,nowTimebb);
double secondes = nowTime - time;
NSString *result = @"";
result = [NSString stringWithFormat:@"%.f秒前",secondes];
if (secondes>60.0) {
result = [NSString stringWithFormat:@"%.f分钟前",secondes/60];
}
if (secondes>60.0*60.0) {
result = [NSString stringWithFormat:@"%.f小时前",secondes/(60*60)];
}
if (secondes>60.0*60.0*24.0) {
result = [NSString stringWithFormat:@"%.f天前",secondes/(60*60*24)];
}
if (secondes>60.0*60.0*24.0*30.0) {
result = [NSString stringWithFormat:@"%.f个月前",secondes/(60*60*24*30)];
}
if (secondes>60.0*60.0*24.0*365.0) {
result = [NSString stringWithFormat:@"%.f年前",secondes/(60*60*24*365)];
}
return result;
}
这个方法一般写在NSString的扩展或类别中。
最后又什么不足之处还请指正,