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

iOS 判断两个日期之间的间隔

时间:2014-08-05 13:37:29      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:des   使用   os   io   for   2014   art   ar   

两个时间段,判断之间的相差,做一些时间范围限制使用

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

    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];

    NSDate* toDate     = [dateFormatter dateFromString:@"20140702142033"];

    NSDate*  startDate    = [ [ NSDate alloc] init ];

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

    NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;

    

    NSDateComponents *cps = [ chineseClendar components:unitFlags fromDate:startDate  toDate:toDate  options:0];

    

    NSInteger diffYear = [cps year];

    NSInteger diffMon = [cps month];

    NSInteger diffDay = [cps day];

    NSInteger diffHour = [cps hour];

    NSInteger diffMin = [cps minute];

    NSInteger diffSec = [cps second];

    NSLog(@" From Now to %@, diff: Years: %d  Months: %d, Days; %d, Hours: %d, Mins:%d, sec:%d", [toDate description], diffYear, diffMon, diffDay, diffHour, diffMin,diffSec );

iOS 判断两个日期之间的间隔,布布扣,bubuko.com

iOS 判断两个日期之间的间隔

标签:des   使用   os   io   for   2014   art   ar   

原文地址:http://www.cnblogs.com/Ewenblog/p/3891791.html

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