比较两个日期的前后顺序

    

 NSString * deliverydate = dicUrl[@"deliverydate"];           
 NSString * arrivedate = dicUrl[@"arrivedate"];

 if (![self compareDate:deliverydate withDate:arrivedate]) {
         [self createUIAlertController:@"送达日期不能小于发车日期"];
  }

-(BOOL)compareDate:(NSString*)date01 withDate:(NSString*)date02{
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *dt1 = [[NSDate alloc] init];
    NSDate *dt2 = [[NSDate alloc] init];
    dt1 = [df dateFromString:date01];
    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending: return YES; break;
            //date02比date01小
        case NSOrderedDescending: return NO; break;
            //date02=date01
        case NSOrderedSame: return YES; break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
    }
}

-(void)createUIAlertController:(NSString*)title
{
    UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action =[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction * action1 =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action1];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

比较两个日期的前后顺序_第1张图片

转载于:https://www.cnblogs.com/sayimba/p/5694112.html

你可能感兴趣的:(比较两个日期的前后顺序)