选择日期与当前日期进行比较以及各种正则表达式

1.选择日期与当前日期进行比较

NSString *choiseTime = @"2016-10-01"; //选择的时间

NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象

[dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式,这里可以设置成自己需要的格式

//然后再将选择的时间转化为自己想要的日期(或者你直接可以将选择的日期来进行比较)

NSDate *date =[dateFormat dateFromString:choiseTime ];

NSLog(@"%@",date);

//获取当前时间日期

NSDate *now = [NSDate date];

NSLog(@"%@",now);

//两个日期进行比较

NSComparisonResult result = [date compare:now];

//根据返回结果进行判断

if (result == NSOrderedDescending) {

NSLog(@"This date is the date of the future");

[MBProgressHUD showHudTipStr:@"This date is the date of the future"];

}

else if (result == NSOrderedAscending){

NSLog(@"This date has passed");

[MBProgressHUD showHudTipStr:@"当前时间不能预约"];

}else{

NSLog(@"Both dates are the same");

[MBProgressHUD showHudTipStr:@"当前时间不能预约"];

}

你可能感兴趣的:(选择日期与当前日期进行比较以及各种正则表达式)