I am new to iPhone developer,
i want to compare two Dates, first date will come from Database let say, newDate and second is todays date,
if today is greater then newDate then i want to display alert.
Here is my code snippet,
NSString *newDate = [formatter stringFromDate:st];
NSDate *today=[[NSDate alloc]init];
if ([today compare:newDate] == NSOrderedDescending)
{
NSLog(@"today is later than newDate");
}
else if ([today compare:newDate] == NSOrderedAscending)
{
NSLog(@"today is earlier than newDate");
}
else
{
NSLog(@"dates are the same");
}
but it crashes, and while compiling it also shows me warning here [today compare:newDate]
Any help will be appreciated.
The problem is, that you are comparing strings and dates. If you look at
newDate, you will see that its aNSStringand not aNSDate. If you use thecompare:method onNSDate, both objects have to beNSDate‘s. Looking at your code, it looks likestis theNSDatecorresponding tonewDate(although your naming seems a bit odd), trytodaywith it instead ofnewDate