2014-09-12 115 views
1

我试图比较UIDatePickers选择时间与当前时间。我希望比较UIDatePickers选择的时间与当前设备上的时间。这是我所尝试过的,但并不准确。试图将日期选择器时间与当前时间进行比较ios

NSDate * date = [NSDate date]; 
NSLog(@"DatePicker.date %@ <= date %@",self.DatePicker.date,date); 

if (self.DatePicker.date<=date) { 

    UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Choose a time in the future." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert show]; 
    return; 

} 

然后它退出;

self.DatePicker.date 2014-09-12 15:15:00 +0000 <= date 2014-09-12 15:03:29 +0000 

,并显示AlertView

这个时钟似乎是一个小时关闭也。

回答

4

您无法使用标准的比较运算符如使用对象==<=等。您需要使用适当的方法进行比较,如compare:

if ([self.DatePicker.date compare:date] == NSOrderedDescending) { 
} 

我可能会倒过来。它可能需要是NSOrderedAscending

+0

感谢您的支持! – DevC 2014-09-12 15:33:05

0
  1. 将您的选取器的timeZone属性设置为当前时区。
  2. 当比较NSDate对象,你应该使用compare:
相关问题