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

计算某个日期与当前日期相隔的天数

时间:2014-09-28 12:51:02      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   ar   sp   art   on   c   时间   

//返回0 当天,1昨天,2前天,以此类推

//之前那样用时间差来整除一天的秒数,计算出来的天数有误差

+ (int)getDaysFromAnotherDay:(longlong)date {//date是时间戳

//    return [self getDaysFrom1970:date] - [self getDaysFrom1970:[[NSDate date] timeIntervalSince1970]];

    NSDateComponents *comps = [[NSDateComponentsalloc] init];

    [compssetDay:[NSDateUtilsgetDay:date]];

    [compssetMonth:[NSDateUtilsgetMonth:date]];

    [compssetYear:[NSDateUtilsgetYear:date]];

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

   NSDate *endDate = [[NSDatealloc] init];

   NSDate *startDate = [gregorian dateFromComponents:comps];

    [compsrelease];

    unsignedint unitFlags =NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit;

   NSDateComponents *comps2 = [gregorian components:unitFlags fromDate:startDatetoDate:endDate options:0];

   int days = [comps2 day];

   return days;

}


+ (int)getYear:(longlong)date {

    return [[selfgetNSDateComponents:date] year];

}


+ (int)getMonth:(longlong)date {

    return [[selfgetNSDateComponents:date] month];

}


+ (int)getDay:(longlong)date {

    return [[selfgetNSDateComponents:date] day];

}


+ (NSDateComponents *)getNSDateComponents:(long long)date {

    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *comps = [[self shareNSCalendar] components:unitFlags fromDate:[NSDate dateWithTimeIntervalSince1970:date]];

    return comps;

}



计算某个日期与当前日期相隔的天数

标签:style   color   io   ar   sp   art   on   c   时间   

原文地址:http://blog.csdn.net/dengmeiyu/article/details/39639415

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