In my app I have to compare two times: current (which is 12:44) and given I tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm"];
NSDate *neededDate = [[NSDate alloc] init];
neededDate = [dateFormatter dateFromString:@"14:45"];
if ([neededDate compare:[NSDate date]] == NSOrderedAscending) {
NSLog(@"yes");
} else NSLog(@"no");
[neededDate release];
[dateFormatter release];
But any time I launch it, NSLog says “no”. Please, suggest where I’ m wrong. Thank you in advance.
Because your
neededDatewill be equal to"1970/01/01 14:45".You need to set correct year and date.
You can get current year, month and say, for example, using next approach: