+ (NSString *) getTimeDiffString:(NSTimeInterval) timestamp
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *todate = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSDate *today = [NSDate date];//当前时间
unsigned int unitFlag = NSDayCalendarUnit | NSHourCalendarUnit |NSMinuteCalendarUnit;
NSDateComponents *gap = [cal components:unitFlag fromDate:today toDate:todate options:0];//计算时间差
if (ABS([gap day]) > 0)
{
return [NSString stringWithFormat:@"%ld天前", (long)(ABS([gap day]))];
}else if(ABS([gap hour]) > 0)
{
return [NSString stringWithFormat:@"%ld小时前", (long)(ABS([gap hour]))];
}else
{
if((long)(ABS([gap minute]))==0)
{
return @"刚刚";
}
return [NSString stringWithFormat:@"%ld分钟前", (long)(ABS([gap minute]))];
}
}
网友评论