-1

hello i am trying to calculate days between today and past date 10-05-2015 in objective c. but when i put condition as follows it always going in if condition. what should be the problem in my code?

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

    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful

    [dateFormatter setDateFormat:@"dd-MM-yy hh:mm a"];

    NSDate *Startdate = [[NSDate alloc] init];
    Startdate = [dateFormatter dateFromString:eventstartdate];

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

    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful

    [dateFormatter1 setDateFormat:@"MM-dd-yy hh:mm a"];
    NSDate *CurrentDate = [[NSDate alloc] init];

    CurrentDate = [dateFormatter1 dateFromString:Currentdate];

    NSDate *startDate= Startdate;
    NSDate *endDate = CurrentDate ;
    if ([CurrentDate earlierDate:Startdate])
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *differenceComponents = [calendar components:NSCalendarUnitDay fromDate:startDate toDate:endDate options:0];
        NSInteger numDays = differenceComponents.day;
    }
    else
    {
        UIAlertView *AlertMsg =[[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"Event is scheduled at %@",eventstartdate] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [AlertMsg show];
    }

get same type of date format as 2015-05-21 12:00 for startdate and end date besides the i have given different formats.

is there any problem with my code?

Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
Dhiraj Patel
  • 269
  • 1
  • 13
  • 1
    have you read the documentation on `earlierDate:` ? It seems not. It returns the earlier date of the two... see http://stackoverflow.com/questions/5965044/how-to-compare-two-nsdates-which-is-more-recent for various ways of solving your problem – Volker May 21 '15 at 05:16
  • thanks volker. i haven't read yet. – Dhiraj Patel May 21 '15 at 05:19
  • find solution : if ([date1 compare:date2] == NSOrderedDescending) { NSLog(@"date1 is later than date2"); } else if ([date1 compare:date2] == NSOrderedAscending) { NSLog(@"date1 is earlier than date2"); } else { NSLog(@"dates are the same"); } – Dhiraj Patel May 21 '15 at 05:22

1 Answers1

4

From Docs of Apple : NSDate compare:

Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.

- (NSComparisonResult)compare:(NSDate *)anotherDate

Parameters anotherDate

The date with which to compare the receiver. This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.

Return Value

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

In other words:

If you want to check for same date

if ([date1 compare:date2] == NSOrderedSame) 
{
    NSLog(@"dates are the same");
}

If you want to check for later date

if ([date1 compare:date2] == NSOrderedDescending) 
{
     NSLog(@"date1 is later than date2");
}

If you want to check for earlier date

if ([date1 compare:date2] == NSOrderedAscending) 
{
    NSLog(@"date1 is earlier than date2");
}
Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76