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

ios-计算时间方法

时间:2014-09-02 19:47:55      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:style   os   io   ar   for   sp   on   时间   line   

-(NSString *)countPublishTime:(NSString *)sDate

{

    NSDate *dtNow = [NSDate date];

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

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

    NSDate *date = [dateFormatter dateFromString:sDate];

    if (date == nil) {

        return @"";

    }

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

    unsigned int unitFlags = NSDayCalendarUnit;

    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date toDate:dtNow options:0];

    NSInteger days = [comps day];

    NSInteger hours;// = [comps hour];

    NSString *sTime = nil;

    NSInteger minute;// = [comps minute];

    if (days==0) {

        unitFlags = NSHourCalendarUnit;

        comps = [gregorian components:unitFlags fromDate:date toDate:dtNow options:0];

        hours = [comps hour];

        if (hours == 0) {

            unitFlags = NSMinuteCalendarUnit;

            comps = [gregorian components:unitFlags fromDate:date toDate:dtNow options:0];

            minute = [comps minute];

            if (minute == 0||minute<0) {

                sTime = @"刚刚";

            }

            else sTime = [NSString stringWithFormat:@"%d分钟前",minute];

        }

        else sTime = [NSString stringWithFormat:@"%d小时前",hours];

    }

    else sTime = [NSString stringWithFormat:@"%d天前",days];

    return sTime;

 

}

 

调用    

NSString *sTime =[self countPublishTime:obj.strPara08];

ios-计算时间方法

标签:style   os   io   ar   for   sp   on   时间   line   

原文地址:http://www.cnblogs.com/xm5mao/p/3952098.html

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