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?