iOS开发iOS学习笔记iOS 开发

根据当前时间判断现在发帖时间与当前时间的差值

2016-06-25  本文已影响76人  ailanhou

+(NSString *) compareCurrentTime:(NSString*) timeStr

//

{

/**

格式转换

*/

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

[formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate* compareDate = [formater dateFromString:timeStr];

/**

*  计算时间

*/

NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];

timeInterval = -timeInterval;

long temp = 0;

NSString *result;

if (timeInterval < 60) {

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

}

else if((temp = timeInterval/60) <60){

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

}

else if((temp = temp/60) <24){

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

}

else if((temp = temp/24) <30){

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

}

else if((temp = temp/30) <12){

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

}

else{

temp = temp/12;

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

}

return  result;

}

上一篇下一篇

猜你喜欢

热点阅读