簡體   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