简体   繁体   中英

Trying to compare date picker time to current time ios

Im trying to compare a UIDatePickers chosen time to the current time. I would preferably like to compare the UIDatePickers chosen time to the time on the current device. This is what I have tried but it is not accurate at all.

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;

}

And it logs out;

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

And shows the AlertView

This clocks seem to be an hour off also.

You can't use standard comparison operators such as == or <= , etc. with objects. You need to compare using appropriate methods such as compare: .

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

I may have that backwards. It may need to be NSOrderedAscending .

  1. Set the timeZone property of your picker to the current timeZone.
  2. When comparing NSDate objects, you should use compare:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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