距离现在时间

实例:

NSString*dateStr =@"2014-11-10 13:00:13";

NSDateFormatter* dateFormat = [[NSDateFormatteralloc]init];//实例化一个NSDateFormatter对象

[dateFormatsetDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,要注意跟下面的dateString匹配,否则日起将无效

NSDate*date =[dateFormatdateFromString:dateStr];

NSString*sinceNowStr = [TimecompareCurrentTime:date];

NSLog(@"sinceNowStr ====== %@",sinceNowStr);


方法:

+(NSString*) compareCurrentTime:(NSDate*) compareDate

//

{

NSTimeIntervaltimeInterval = [compareDatetimeIntervalSinceNow];

timeInterval = -timeInterval;

longtemp =0;

NSString*result;

if(timeInterval <60) {

result = [NSStringstringWithFormat:@"刚刚"];

}

elseif((temp = timeInterval/60) <60){

result = [NSStringstringWithFormat:@"%ld分钟前",temp];

}

elseif((temp = temp/60) <24){

result = [NSStringstringWithFormat:@"%ld小时前",temp];

}

elseif((temp = temp/24) <30){

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

}

elseif((temp = temp/30) <12){

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

}

else{

temp = temp/12;

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

}

returnresult;

}

你可能感兴趣的:(距离现在时间)