繁体   English   中英

尝试将日期选择器时间与当前时间ios进行比较

[英]Trying to compare date picker time to current time ios

我正在尝试将UIDatePickers选择的时间与当前时间进行比较。 我最好将UIDatePickers选择的时间与当前设备上的时间进行比较。 这是我尝试过的,但根本不准确。

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

这个时钟似乎也要休息一个小时。

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

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

我可能会倒退。 它可能需要是NSOrderedAscending

  1. 将选择器的timeZone属性设置为当前timeZone。
  2. 比较NSDate对象时,应使用compare:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM