iOS 时间比较大小

转载:http://blog.csdn.net/sunnyboy9/article/details/49889923

/*******

  设置两个时间差为固定值

  *******/


    

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDate *currentDate = [NSDate date];

    NSDateComponents *comps = [[NSDateComponents alloc] init];

    [comps setDay:1];//设置最大时间为:当前时间推后1

    NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

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

    formater.dateFormat = @"yyyy-MM-dd HH:mm";

   



/*******

  日期可以进行比较以确定大小或相等,也可以确定两个日期之间的时间间隔。两个日期的间隔时间差可以使用-timeIntervalSinceDate:方法来计算

  *******/


   NSDate * now = [NSDatedate];

  NSDate * anHourAgo = [nowdateByAddingTimeInterval:-60*60];

  NSTimeInterval timeBetween = [nowtimeIntervalSinceDate:anHourAgo];

  NSLog(@"%f",timeBetween);//结果:3600.000000



/*******

 日期比较也可以使用-timeIntervalSinceNow方法获取和当前的时间间隔

*********/


   NSDate * anHourago = [NSDatedateWithTimeIntervalSinceNow:-60*60];

  NSTimeInterval interval = [anHouragotimeIntervalSinceNow];

  NSLog(@"%f",interval);//结果: -3600.000007



/****

 iOS比较日期大小默认会比较到秒

 ****/

+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay

{

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]init];

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

    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];

    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];

    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];

    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];

    NSComparisonResult result = [dateA compare:dateB];

    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);

    if (result == NSOrderedDescending) {

        //NSLog(@"Date1  is in the future");

        return 1;

    }

    else if (result ==NSOrderedAscending){

        //NSLog(@"Date1 is in the past");

        return -1;

    }

    //NSLog(@"Both dates are the same");

    return 0;

    

}

/************

 日期格式请传入:2013-08-05 12:12:12;如果修改日期格式,比如:2013-08-05,则将[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];修改为[df setDateFormat:@"yyyy-MM-dd"];

 ***********/

+(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{

    int ci;

    NSDateFormatter *df = [[NSDateFormatteralloc]init];

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

    NSDate *dt1 = [[NSDatealloc]init];

    NSDate *dt2 = [[NSDatealloc]init];

    dt1 = [df dateFromString:date01];

    dt2 = [df dateFromString:date02];

    NSComparisonResult result = [dt1 compare:dt2];

    switch (result)

    {

            //date02date01

        caseNSOrderedAscending: ci=1;break;

            //date02date01

        caseNSOrderedDescending: ci=-1;break;

            //date02=date01

        case NSOrderedSame: ci=0;break;

        defaultNSLog(@"erorr dates %@, %@", dt2, dt1);break;

    }

    return ci;

}

/*********

 比较两个日期之间的差值

 *******/

+ (instancetype)differencewithDate:(NSString*)dateString withDate:(NSString*)anotherdateString{

    {

        // _created_at == Thu Oct 16 17:06:25 +0800 2014

        // dateFormat == EEE MMM dd HH:mm:ss Z yyyy

        // NSString --> NSDate

        NSDateFormatter *fmt = [[NSDateFormatteralloc]init];

        // 如果是真机调试,转换这种欧美时间,需要设置locale

        fmt.locale = [[NSLocalealloc]initWithLocaleIdentifier:@"en_US"];

        

        // 设置日期格式(声明字符串里面每个数字和单词的含义)

        

        // E:星期几

        // M:月份

        // d:几号(这个月的第几天)

        // H:24小时制的小时

        // m:分钟

        // s:

        // y:

        fmt.dateFormat =@"EEE MMM dd HH:mm:ss Z yyyy";

        

        // 微博的创建日期

        NSDate *createDate = [fmt dateFromString:dateString];

        // 当前时间

        NSDate *now = [NSDatedate];

        // 日历对象(方便比较两个日期之间的差距)

        NSCalendar *calendar = [NSCalendarcurrentCalendar];

        // NSCalendarUnit枚举代表想获得哪些差值

        NSCalendarUnit unit =NSCalendarUnitYear |NSCalendarUnitMonth|NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute|NSCalendarUnitSecond;

        // 计算两个日期之间的差值

        NSDateComponents *cmps = [calendar components:unit fromDate:createDate toDate:now options:0];

        

        NSLog(@"%@ %@ %@", createDate, now, cmps);

        

        return cmps;

    }



/*********

 比较两个日期之间某年或某月或某日或某时等的具体差值

 *******/

+ (instancetype)differencewithDate:(NSString*)dateString withDate:(NSString*)anotherdateString{

    {

        

    NSDateFormatter * formatter = [[NSDateFormatteralloc]init];

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

    [formatter setDateFormat:@"yyyy-MM-dd "];

    NSDate  *date2 = [formatterdateFromString :dateString];

    NSDate  *date1 = [formatterdateFromString :anotherdateString];

    //     NSLog(@"------date1=%@   date2=%@------%@",date1, date2);

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

    unsignedint unitFlags =NSMonthCalendarUnit;//年、月、日、时、分、秒、周等等都可以

    NSDateComponents *comps = [gregoriancomponents:unitFlagsfromDate:date1toDate:date2options:0];

    int month = [compsmonth];//时间差

    NSLog(@"时间差 = %dabs(month)=%d",month,abs(month));

        return month;

    }




 NSDate 转换为 NSString:

  1. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  2. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  3. NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
  4. NSLog(@"%@", strDate);
  5. [dateFormatter release];


结果:

  1. 2010-08-04 16:01:03



 NSString 转换为 NSDate:

  1. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  2. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  3. NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
  4. NSLog(@"%@", date);
  5. [dateFormatter release];


结果:

  1. 2010-08-04 16:01:03 +0800


日期之间比较可用以下方法

    - (BOOL)isEqualToDate:(NSDate *)otherDate;

    otherDate比较,相同返回YES

 

    - (NSDate *)earlierDate:(NSDate *)anotherDate;

    anotherDate比较,返回较早的那个日期

 

    - (NSDate *)laterDate:(NSDate *)anotherDate;

    anotherDate比较,返回较晚的那个日期

 

    - (NSComparisonResult)compare:(NSDate *)other;

    该方法用于排序时调用:

      . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame

      . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

      . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值