我試圖強制用戶選擇一個日期在未來與日期選擇器。我顯然使用了compare方法,但是,當我執行下面的代碼時,即使它與[NSDate date]的日期相同,它也會告訴執行if語句。這裏是我的代碼:比較NSDate [NSDate日期]
if ([datePicker.date compare:[NSDate date]] == NSOrderedAscending) // If the picked date is earlier than today
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hmm, this date is earlier than today"
message:@"The date you've selected is earlier than today's date, please pick another"
delegate:nil
cancelButtonTitle:@"Okay, I'll pick another"
otherButtonTitles:nil];
// Show the alert
[alert show];
// Release the alert
[alert release];
}
您是否嘗試過使用'laterDate:'(或'earlierDate:')做比較? – gerry3 2010-02-13 19:25:20
如果你NSLog日期只是爲了看看它是否有正確的值,會發生什麼。 – 2010-02-13 19:43:10