NSDate日期比较

作者: FlyElephant | 来源:发表于2016-05-04 23:43 被阅读716次

    NSDate的格式默认是的含有时分秒,关于NSDate通过Compare方法比较即可:

    NSDate *currentDate=[NSDate date];
    NSString *dateString = @"2016-05-04 16:59:00";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *mydate=[formatter dateFromString:dateString];
    NSComparisonResult result=[currentDate compare:mydate];
    switch (result) {
        case NSOrderedAscending: {
            NSLog(@"FlyElephant:%@--时间小于---%@",currentDate,mydate);
            break;
        }
        case NSOrderedSame: {
            NSLog(@"FlyElephant:%@--时间等于---%@",currentDate,mydate);
            break;
        }
        case NSOrderedDescending: {
            NSLog(@"FlyElephant:%@--时间大于---%@",currentDate,mydate);
            break;
        }
    }
    

    输出结果:
    FlyElephant:2016-05-04 15:27:55 +0000--****时间大于****---2016-05-04 08:59:00 +0000

    如果处理过时间,有可能会遇到,我们只是比较日期是相同,不需要比较小时分钟和秒数,除了将dateformate的格式修改为yyyy-MM-dd形式之后,我们还有NSDateComponents方式实现:

    NSString *fromDateStr= @"2016-05-05 06:00:00";
    NSString *toDateStr= @"2016-05-05 8:59:00";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromDate=[formatter dateFromString:fromDateStr];
    NSDate *toDate=[formatter dateFromString:toDateStr];
    if([fromDate isEqualTo:toDate]){
        NSLog(@"FlyElephant:%@---等于---%@",fromDate,toDate);
    }
    
    toDate=[[NSDate date] dateByAddingTimeInterval:60*60*100];
    if([fromDate isEarlierThan:toDate]){
        NSLog(@"FlyElephant:%@---小于---%@",fromDate,toDate);
    }
    toDate=[fromDate dateByAddingTimeInterval:60*60];
    if ([fromDate isEarlierOrEqual:toDate]) {
        NSLog(@"FlyElephant:%@---小于等于---%@",fromDate,toDate);
    }
    
    toDate=[[NSDate date] dateByAddingTimeInterval:-60*60*100];
    if([fromDate isLaterThan:toDate]){
        NSLog(@"FlyElephant:%@---大于---%@",fromDate,toDate);
    }
    
    toDate=[fromDate dateByAddingTimeInterval:-60*60];
    if ([fromDate isLaterThanOrEqual:toDate]) {
        NSLog(@"FlyElephant:%@---大于等于---%@",fromDate,toDate);
    }
    

    时间比较结果:
    <pre>
    **2016-05-04 23:38:44.754 FECategory[31727:405193] FlyElephant:2016-05-04 22:00:00 +0000---****等于****---2016-05-05 00:59:00 +0000** **2016-05-04 23:38:44.754 FECategory[31727:405193] FlyElephant:2016-05-04 22:00:00 +0000---****小于****---2016-05-08 19:38:44 +0000** **2016-05-04 23:38:44.755 FECategory[31727:405193] FlyElephant:2016-05-04 22:00:00 +0000---****小于等于****---2016-05-04 23:00:00 +0000** **2016-05-04 23:38:44.755 FECategory[31727:405193] FlyElephant:2016-05-04 22:00:00 +0000---****大于****---2016-04-30 11:38:44 +0000** **2016-05-04 23:38:44.756 FECategory[31727:405193] FlyElephant:2016-05-04 22:00:00 +0000---****大于等于****---2016-05-04 21:00:00 +0000**
    </pre>

    时间分类实现:
    <pre>`
    //
    // NSDate+FEDateCompare.m
    // FELabel
    //
    // Created by FlyElephant on 16/5/4.
    // Copyright © 2016年 FlyElephant. All rights reserved.
    //

    import "NSDate+FEDateCompare.h"

    @implementation NSDate (FEDateCompare)

    -(NSComparisonResult)compareDateByDay:(NSDate *)date{

    NSDate *fromDate=[self formatDateByComponent:self];
    NSDate *toDate=[self formatDateByComponent:date];
    
    NSComparisonResult result=[fromDate compare:toDate];
    switch (result) {
        case NSOrderedAscending: {
            return NSOrderedAscending;
            break;
        }
        case NSOrderedSame: {
            return NSOrderedSame;
            break;
        }
        case NSOrderedDescending: {
            return NSOrderedDescending;
            break;
        }
    }
    

    }

    -(BOOL)isLaterThanOrEqual:(NSDate *)date{
    NSDate *fromDate=[self formatDateByComponent:self];
    NSDate *toDate=[self formatDateByComponent:date];
    return !([fromDate compare:toDate]==NSOrderedAscending);
    }

    -(BOOL)isLaterThan:(NSDate *)date{
    NSDate *fromDate=[self formatDateByComponent:self];
    NSDate *toDate=[self formatDateByComponent:date];
    return ([fromDate compare:toDate]==NSOrderedDescending);
    }

    -(BOOL)isEarlierOrEqual:(NSDate *)date{
    NSDate *fromDate=[self formatDateByComponent:self];
    NSDate *toDate=[self formatDateByComponent:date];
    return !([fromDate compare:toDate]==NSOrderedDescending);
    }

    -(BOOL)isEarlierThan:(NSDate *)date{
    NSDate *fromDate=[self formatDateByComponent:self];
    NSDate *toDate=[self formatDateByComponent:date];
    return ([fromDate compare:toDate]==NSOrderedAscending);
    }

    -(BOOL)isEqualTo:(NSDate *)date{
    NSDate *fromDate=[self formatDateByComponent:self];
    NSDate *toDate=[self formatDateByComponent:date];
    return ([fromDate compare:toDate]==NSOrderedSame);
    }

    pragma mark - private

    -(NSDate *)formatDateByComponent:(NSDate *)date{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    return [cal dateFromComponents:components];
    }

    @end
    `</pre>

    相关文章

      网友评论

      本文标题:NSDate日期比较

      本文链接:https://www.haomeiwen.com/subject/zbqdrttx.html