码迷,mamicode.com
首页 > 移动开发 > 详细

iOS一个时间距现在的时间方法 intervalSinceNow NSDateFormatter

时间:2016-08-24 11:05:17      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

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

{

    NSArray* timeArray = [theDate componentsSeparatedByString:@"."];

    theDate = [timeArray objectAtIndex:0];

 

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

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

    NSDate* d = [date dateFromString:theDate];

 

    NSTimeInterval late = [d timeIntervalSince1970] * 1;

 

    NSDate* dat = [NSDate date];

    NSTimeInterval now = [dat timeIntervalSince1970] * 1;

    NSString* timeString = @"";

 

    NSTimeInterval cha = now - late;

 

    if (cha / 3600 < 1) {

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

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

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

    }

    else if (cha / 86400 < 1) {

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

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

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

    }

    else if (cha / (86400 * 30) < 1) {

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

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

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

    }

    else if (cha / (86400 * 30 * 12) < 1) {

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

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

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

    }

    else {

        timeString = [NSString stringWithFormat:@"%f", cha / (86400 * 30 * 12)];

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

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

    }

    return timeString;

}

 

iOS一个时间距现在的时间方法 intervalSinceNow NSDateFormatter

标签:

原文地址:http://www.cnblogs.com/yangdahong/p/5801966.html

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