I’m new to iOS programming and I’ve been looking a lot for a way to compare “today” (an NSDate) to a formatted string date (dd/MM/yyyy).
I’ve found some good answers with the NSDate’s earlierDate, laterDate etc. but the code doesn’t seem to work in my project.
here it is :
// [book quandd] is the string : 25/02/2011 (or whatever dd/MM/yyyy date)
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
if ( [[dateFormat dateFromString:[book quandd]] isEqualToDate:today]) {
NSLog(@"Dates are equal");
}
if ( [[dateFormat dateFromString:[book quandd]] earlierDate:today]) {
NSLog(@"earlier");
}
if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]) {
NSLog(@"later");
}
Whatever I do and whatever the date in [book quandd], the console always writes “earlier” and “later”, as if they were later and earlier at the same time than today.
Where is that “bug” coming from ?
Is there some easy/easier way to compare the today date with 26/02/2011 for example ?
Use
timeIntervalSinceNow:to get the diff from the necessary date.You should also try to get the difference between them and then convert to a string.