Last active
December 30, 2015 08:19
-
-
Save HamGuy/7801441 to your computer and use it in GitHub Desktop.
这些时间从API解析下来的时间格式都为 yyyy-MM-dd HH:mm:ss(比如:2013-03-09 09:51:22),通过在API上获取文章时间后经过算法转换而得到,在客户端源码的Tool.m中+ (NSString *)intervalSinceNow: (NSString *) theDate 实现这一个方法: 如果信息发表在1小时之前显示分钟,一天之内的信息显示小时,10天之内按天显示,10天之外安所发表时间进行显示: source:http://blog.csdn.net/duxinfeng2010/article/details/8653411
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (NSString *)intervalSinceNow: (NSString *) theDate | |
{ | |
NSDateFormatter *date=[[NSDateFormatter alloc] init]; | |
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; | |
NSDate *d=[date dateFromString:theDate]; | |
NSTimeInterval late=[d timeIntervalSince1970]*1; | |
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; | |
NSTimeInterval now=[dat timeIntervalSince1970]*1; | |
NSString *timeString=@""; | |
NSTimeInterval cha=now-late; | |
// 发表在一小时之内 | |
if (cha/3600<1) { | |
if (cha/60<1) { | |
timeString = @"1"; | |
} | |
else | |
{ | |
timeString = [NSString stringWithFormat:@"%f", cha/60]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
} | |
timeString=[NSString stringWithFormat:@"%@分钟前", timeString]; | |
} | |
// 在一小时以上24小以内 | |
else if (cha/3600>1&&cha/86400<1) { | |
timeString = [NSString stringWithFormat:@"%f", cha/3600]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
timeString=[NSString stringWithFormat:@"%@小时前", timeString]; | |
} | |
// 发表在24以上10天以内 | |
else if (cha/86400>1&&cha/864000<1) | |
{ | |
timeString = [NSString stringWithFormat:@"%f", cha/86400]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
timeString=[NSString stringWithFormat:@"%@天前", timeString]; | |
} | |
// 发表时间大于10天 | |
else | |
{ | |
// timeString = [NSString stringWithFormat:@"%d-%"] | |
NSArray *array = [theDate componentsSeparatedByString:@" "]; | |
// return [array objectAtIndex:0]; | |
timeString = [array objectAtIndex:0]; | |
} | |
return timeString; | |
} | |
//改良版 | |
- (NSString *)intervalSinceNow | |
{ | |
NSDateFormatter *date=[[NSDateFormatter alloc] init]; | |
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; | |
NSTimeInterval timeInterval=[self timeIntervalSince1970]*1; | |
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; | |
NSTimeInterval now=[dat timeIntervalSince1970]*1; | |
NSString *timeString=@""; | |
NSTimeInterval cha=now-timeInterval; | |
// 发表在一小时之内 | |
if (cha/3600<1) { | |
if (cha/60<1) { | |
timeString = @"刚刚"; | |
} | |
else | |
{ | |
timeString = [NSString stringWithFormat:@"%f", cha/60]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
timeString=[NSString stringWithFormat:@"%@分钟前", timeString]; | |
} | |
} | |
// 在一小时以上24小以内 | |
else if (cha/3600>1&&cha/86400<1) { | |
timeString = [NSString stringWithFormat:@"%f", cha/3600]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
timeString=[NSString stringWithFormat:@"%@小时前", timeString]; | |
} | |
// 发表在24以上10天以内 | |
else if (cha/86400>1&&cha/864000<1) | |
{ | |
timeString = [NSString stringWithFormat:@"%f", cha/86400]; | |
timeString = [timeString substringToIndex:timeString.length-7]; | |
timeString=[NSString stringWithFormat:@"%@天前", timeString]; | |
} | |
// 发表时间大于10天 | |
else | |
{ | |
NSArray *array = [[date stringFromDate:self] componentsSeparatedByString:@" "]; | |
timeString = [array objectAtIndex:0]; | |
} | |
return timeString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment