美文网首页
计算2个时间 相差分钟数

计算2个时间 相差分钟数

作者: 呦嚯嚯嚯12138 | 来源:发表于2016-10-03 15:55 被阅读12次

    + (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]))];

    }

    }

    相关文章

      网友评论

          本文标题:计算2个时间 相差分钟数

          本文链接:https://www.haomeiwen.com/subject/rohjyttx.html