1

I've read all the questions and answer and all the tutorial about this subject, but for some reason it's not working for me. always showing me that the two dates are the same date!

Please some one help me to figure it out, I just want to check if one is bigger than the other (including date and time - without seconds) or if they are equal.

This is my code:

- (BOOL)isEndDateIsBiggerThanCurrectDate:(NSDate *)checkEndDate
{
    NSString *endd = [NSDateFormatter localizedStringFromDate:checkEndDate
                                                      dateStyle:NSDateFormatterShortStyle
                                                      timeStyle:NSDateFormatterShortStyle];

    NSString *curreeeent = [NSDateFormatter localizedStringFromDate:[NSDate date]
                                                      dateStyle:NSDateFormatterShortStyle
                                                      timeStyle:NSDateFormatterShortStyle];

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

    NSDate * newCurrent = [df dateFromString:endd];
    NSDate * newEnd = [df dateFromString:curreeeent];

    switch ([newCurrent compare:newEnd])
    {
        case NSOrderedAscending:
            return YES;
            break;
        case NSOrderedSame:
            return NO;
            break;
        case NSOrderedDescending:
            return NO;
            break;
    }
}

Thank you very much!

ytpm
  • 4,962
  • 6
  • 56
  • 113

4 Answers4

1

For this, you have to use NSCalender.

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit);


NSDateComponents *firstComponents = [calendar components:desiredComponents fromDate:[NSDate date]];
NSDateComponents *secondComponents = [calendar components:desiredComponents fromDate: checkEndDate];

NSDate *first = [calendar dateFromComponents:firstComponents];
NSDate *second = [calendar dateFromComponents:secondComponents];

NSComparisonResult result = [first compare:second];
if (result == NSOrderedAscending) {
    //checkEndDate is before now
} else if (result == NSOrderedDescending) {
    //checkEndDate is after now
}  else {
    //both are same
}
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
0

You should really be using time intervals rather than converting between dates and strings.

Something like the following should suit your needs:

//current time
NSDate *now = [NSDate date];

//time in the future
NSDate *distantFuture = [NSDate distantFuture];

//gather time interval
if([now timeIntervalSinceDate:distantFuture] > 0)
{
    //huzzah!
}
Zack Brown
  • 5,990
  • 2
  • 41
  • 54
0

I've got the answer, just checking the exact time between two dates and compare it.

- (BOOL)isEndDateIsSmallerThanCurrent:(NSDate *)checkEndDate
{
    NSDate* enddate = checkEndDate;
    NSDate* currentdate = [NSDate date];
    NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
    double secondsInMinute = 60;
    NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;

    if (secondsBetweenDates == 0)
        return YES;
    else if (secondsBetweenDates < 0)
        return YES;
    else
        return NO;
}
ytpm
  • 4,962
  • 6
  • 56
  • 113
-2

Why don't you change the dates into time interval since 1970 and sort by that. Extremely simple number compare, much quicker than string compare, and they will always sort correct, not like 1,10,11,2,21,22,3,....

NSDate *now = [NSDate date];
NSTimeInterval ti = [now timeIntervalSince1970];

Thats it. No new object creations, much quicker and much less taxing on the cpu.

See here how you get rid of seconds, but it is easy because you have numbers, for seconds. See here How to set seconds to zero for NSDate

Community
  • 1
  • 1
Trausti Thor
  • 3,722
  • 31
  • 41
  • 1
    `NSTimeInterval` is **not** an integer but a double. – JustSid Oct 23 '12 at 09:29
  • NSTimeInterval will use seconds and milliseconds whilst the question is about bein equal in the same minute – mmmmmm Oct 23 '12 at 09:31
  • Depending on how you create the date, the seconds, and milliseconds can be zero. If you create a date for ex. 23-12-2012 12:00, you get no redundant seconds or milli seconds – Trausti Thor Oct 23 '12 at 09:34
  • @TraustiThor so how can I do that? – ytpm Oct 23 '12 at 09:34
  • Well, as my answer told you, when you have nstimeinterval, you have a plain number, easiest thing in the world. Here is your answer : http://stackoverflow.com/questions/1525825/set-seconds-to-zero-for-nsdate – Trausti Thor Oct 23 '12 at 09:45
  • Also note that there are other functions available to change dates into time intervals, so you are not forced to use from 1970, although it really doesn't matter even if you are working with dates earlier than that – Trausti Thor Oct 23 '12 at 09:47