与ios中的开始日期和结束日期进行比较

时间:2014-12-22 07:15:40

标签: ios date

在我的ViewController中有2个TextFields开始日期和结束日期。 我正在使用UIDatePicker访问Textfield值,一切都很好。 但我希望检查开始日期始终小于或等于结束日期。 请任何人帮助我......我真诚地感谢你。

@interface: UIViewController
{
}
UIDatePicker *itsDatePicker1;
UIDatePicker *itsDatePicker2;
NSDateFormatter *dateFormatter1;
NSDateFormatter *dateFormatter2;
@property(nonatomic,weak) IBOutlet UITextField *checkInDateTxtFld;
@property(nonatomic,weak) IBOutlet UITextField *checkOutDateTxtFld;
- (void)viewDidLoad 
{
    [super viewDidLoad];

}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (self.activeTextFld)
    {
        [self.activeTextFld resignFirstResponder];
    }
    self.activeTextFld=textField;

    if (textField.tag==12)
    {

        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        toolbar.barStyle   = UIBarStyleBlackTranslucent;

        UIBarButtonItem *itemDone  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.checkInDateTxtFld action:@selector(resignFirstResponder)];
        UIBarButtonItem *itemSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        toolbar.items = @[itemSpace,itemDone];

        itsDatePicker1= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
        itsDatePicker1.minimumDate   = [NSDate date];
        itsDatePicker1.date          = [NSDate date];
        [itsDatePicker1 addTarget:self action:@selector(incidentDateValueChanged1:) forControlEvents:UIControlEventValueChanged];
        self.checkInDateTxtFld.inputAccessoryView = toolbar;
        self.checkInDateTxtFld.inputView          = itsDatePicker1;
    }
    else if (textField.tag==13)
    {
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        toolbar.barStyle   = UIBarStyleBlackTranslucent;

        UIBarButtonItem *itemDone  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.checkOutDateTxtFld action:@selector(resignFirstResponder)];
        UIBarButtonItem *itemSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        toolbar.items = @[itemSpace,itemDone];

        itsDatePicker2= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
        itsDatePicker2.minimumDate   = [NSDate date];
        itsDatePicker2.date          = [NSDate date];
        [itsDatePicker2 addTarget:self action:@selector(incidentDateValueChanged2:) forControlEvents:UIControlEventValueChanged];
        self.checkOutDateTxtFld.inputAccessoryView = toolbar;
        self.checkOutDateTxtFld.inputView = itsDatePicker2;
    }
    else if (textField.tag==14)
    {
        [textField resignFirstResponder];
        self.selctedPickerType=CHECK_NO_OF_ROOMS;
        [self showPicker];
    }
}

- (IBAction) incidentDateValueChanged1:(id)sender
{
    dateFormatter1 = [[NSDateFormatter alloc] init];
   // [dateFormatter1 setDateFormat:@"MMM d, yyyy"];
    [dateFormatter1 setDateFormat:@"MM-dd-yyyy"];

    self.checkInDateTxtFld.text = [dateFormatter1 stringFromDate:[itsDatePicker1 date]];
}

- (IBAction) incidentDateValueChanged2:(id)sender
{
    dateFormatter2 = [[NSDateFormatter alloc] init];
    //  [dateFormatter2 setDateFormat:@"MMM d, yyyy"];
    [dateFormatter2 setDateFormat:@"MM-dd-yyyy"];
    self.checkOutDateTxtFld.text = [dateFormatter2 stringFromDate:[itsDatePicker2 date]];
}

2 个答案:

答案 0 :(得分:0)

非常简单,只需用日期检查

即可
  

date1和date2都是NSDate的对象。一个具有今日日期的值,另一个是您的给定日期。并通过

进行简单比较
        if ([date1 compare:date2] == NSOrderedSame) {
            return YES;
        } else {
            return NO;
        }

答案 1 :(得分:0)

if ([date1 compare:date2] == NSOrderedDescending) {
    NSLog(@"date1 is Newer than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
    NSLog(@"date1 is earlier than date2");
} else {
    NSLog(@"dates are the Equal");
}