Monday, 19 December 2011

Convert NSString into NSDate and Compare two NSDates



NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd"];

 NSDate *date1 = [formatter dateFromString:@"2011-12-19"];
 NSDate *date2 = [formatter dateFromString:@"2011-12-15"];

NSComparisonResult result;
result = [date1 compare:date2];

if (result==NSOrderedAscending) {

NSLog(@"date2 is in the future");

} else if (result==NSOrderedDescending) {

NSLog(@"date2 is in the past");

} else {

NSLog(@"Both dates are the same");
}

No comments:

Post a Comment