How to compare two NSDate objects in objective C

后端 未结 6 1950
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 00:51

I have objects of Date type.

I want to compare them, and I have written an if condition in the following way:

if (([startDate1 isEqualToDate:[self g         


        
相关标签:
6条回答
  • 2020-11-30 01:00

    This is a simple way to check if the date you think is larger (eg. in the future) compared to the date you think is earlier. Here is what this does.

    1. Gets the time interval (seconds and frac seconds) of both current and future date
    2. returns true if the date your testing is greater (time reference) the the other date

    Here you go

    - (BOOL)isDateGreaterThanDate:(NSDate*)greaterDate and:(NSDate*)lesserDate{
    
        NSTimeInterval greaterDateInterval = [greaterDate timeIntervalSince1970];
        NSTimeInterval lesserDateInterval = [lesserDate timeIntervalSince1970];
    
        if (greaterDateInterval > lesserDateInterval) 
            return YES;
    
        return NO;
    }
    

    This also works

    BOOL *isGreaterDateTruelyGreater = [greaterDate timeIntervalSince1970] > [lesserDate timeIntervalSince1970];
    

    The way objective-c deals with dates, is less than intuitive. Basically you can use NSTimeInterval to get the number of seconds from a point in time. It usually gives a long number, for instance as i am writing this 1351026414.640865 is the time interval from 1970.

    Point in time > point in time 1351026415.640865 > 1351026414.640865 The above is 1 second ahead, so it is true.

    0 讨论(0)
  • 2020-11-30 01:06

    Easy way to compare NSDates (found here http://webd.fr/637-comparer-deux-nsdate):

    #import <Foundation/Foundation.h>
    
    @interface NSDate (Compare)
    
    -(BOOL) isLaterThanOrEqualTo:(NSDate*)date;
    -(BOOL) isEarlierThanOrEqualTo:(NSDate*)date;
    -(BOOL) isLaterThan:(NSDate*)date;
    -(BOOL) isEarlierThan:(NSDate*)date;
    //- (BOOL)isEqualToDate:(NSDate *)date; already part of the NSDate API
    
    @end
    

    And the implementation:

    #import "NSDate+Compare.h"
    
    @implementation NSDate (Compare)
    
    -(BOOL) isLaterThanOrEqualTo:(NSDate*)date {
        return !([self compare:date] == NSOrderedAscending);
    }
    
    -(BOOL) isEarlierThanOrEqualTo:(NSDate*)date {
        return !([self compare:date] == NSOrderedDescending);
    }
    -(BOOL) isLaterThan:(NSDate*)date {
        return ([self compare:date] == NSOrderedDescending);
    
    }
    -(BOOL) isEarlierThan:(NSDate*)date {
        return ([self compare:date] == NSOrderedAscending);
    }
    
    @end
    

    Simple to use:

    if([aDateYouWant ToCompare isEarlierThanOrEqualTo:[NSDate date]]) // [NSDate date] is now
    {
        // do your thing ...
    }
    

    Enjoy

    0 讨论(0)
  • 2020-11-30 01:09

    This link shows it in the best way:

    http://www.myuiviews.com/2011/01/10/nsdate-comparison-which-is-older.html

    0 讨论(0)
  • 2020-11-30 01:24

    Here's an example using the NSDate method, compare:

    if([startDate compare: endDate] == NSOrderedDescending) // if start is later in time than end
    {
        // do something
    }
    

    From the class reference:

    If...

    The receiver and anotherDate are exactly equal to each other, NSOrderedSame.

    The receiver is later in time than anotherDate, NSOrderedDescending.

    The receiver is earlier in time than anotherDate, NSOrderedAscending.

    You could also use the timeIntervalSinceDate: method if you wanted to compare two dates and find the seconds between them, for example.

    EDIT:

    if(([startDate1 compare:[self getDefaultDate]] == NSOrderedSame) || ([startDate1 compare: [self getDefaultDate]] != NSOrderedSame && (([m_selectedDate compare: m_currentDate1] == NSOrderedDescending) || [m_selectedDate compare: m_currentDate1] == NSOrderedSame) && cycleStopped))
    
    0 讨论(0)
  • 2020-11-30 01:24

    I must admit that I find the "compare" == NSWhatHaveYou business too complicated to remember (even though it's correct and it works). NSDate has two other methods that my brain finds much easier to deal with: earlierDate and laterDate.

    Consider this:

    if ([myDate laterDate:today] == myDate) {
        NSLog(@"myDate is LATER than today");
    } else {
        NSLog(@"myDate is EARLIER than today");
    }
    
    // likewise:
    if ([myDate earlierDate:today] == myDate) {
        NSLog(@"myDate is EARLIER than today");
    } else {
        NSLog(@"myDate is LATER than today");
    }
    

    Either method returns an NSDate object, which is either earlier or later than the other.

    http://pinkstone.co.uk/how-to-compare-two-nsdates/

    0 讨论(0)
  • 2020-11-30 01:26

    I really liked Luke's answer, and it worked like a charm.

    I used something like this:

    If ([enteredDate compare: [NSDate Date]]== NSOrderedSame || NSOrderedDescending)
    {
     //Do Something if the enteredDate is later or the same as today
    }else
    {
    //if the enteredDate is before today's date
    }
    

    I'm not sure if that helps, but just to reiterate what Luke had written.

    0 讨论(0)
提交回复
热议问题