码迷,mamicode.com
首页 > 其他好文 > 详细

计算时间差

时间:2015-08-14 13:10:21      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:

- (NSString *)intervalSinceNow: (NSString *) theDate

{

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

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

    NSDate * oldDate=[date dateFromString:theDate];

    NSTimeInterval temp1=[oldDate timeIntervalSince1970]*1;

    NSDate* nowDate = [NSDate dateWithTimeIntervalSinceNow:0];

    NSTimeInterval temp12=[nowDate timeIntervalSince1970];

    NSString *timeString=@"";

    NSTimeInterval cha=temp2-temp1;

    if (cha/3600<1) {

        timeString = [NSString stringWithFormat:@"%f", cha/60];

        timeString = [timeString substringToIndex:timeString.length-7];

        timeString=[NSString stringWithFormat:@"%@分钟前", timeString];

    }

    if (cha/3600>1&&cha/86400<1) {

        timeString = [NSString stringWithFormat:@"%f", cha/3600];

        timeString = [timeString substringToIndex:timeString.length-7];

        timeString=[NSString stringWithFormat:@"%@小时前", timeString];

    }

    if (cha/86400>1)

    {

        timeString = [NSString stringWithFormat:@"%f", cha/86400];

        timeString = [timeString substringToIndex:timeString.length-7];

        timeString=[NSString stringWithFormat:@"%@天前", timeString];

    }

    return timeString;

}

计算时间差

标签:

原文地址:http://www.cnblogs.com/changeUniversal/p/4729492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!