比较NSDate和[NSDate date] [英] Comparing an NSDate to [NSDate date]

查看:137
本文介绍了比较NSDate和[NSDate date]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制用户使用日期选择器选择日期。我明显使用compare:方法,但是,当我做以下代码,即使它是与[NSDate日期]相同的日期,它告诉执行if语句。这是我的代码:

  if([datePicker.date compare:[NSDate date]] == NSOrderedAscending)// If the挑选的日期早于今天
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@嗯,这个日期早于今天
message:@选择的时间早于今天的日期,请选择另一个
delegate:nil
cancelButtonTitle:@好的,我会选择另一个
otherButtonTitles:nil。
//显示提醒
[alert show];

//发布警报
[alert release];
}


解决方案

只要使用 UIDatePicker minimumDate 属性,就可以解决问题,这甚至是一个更清洁的方法。


I am trying to force a user to select a date in the future with a date picker. I obviously used the compare: method, however, when I do the following code, even if it's the same date as [NSDate date], it tells the executes the if statement. Here is my code:

    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];
}

解决方案

I don't know the answer to your problem, but how you could circumvent it.

Just use the minimumDate property of the UIDatePicker, that's even a much cleaner approach.

这篇关于比较NSDate和[NSDate date]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆