我需要将当前日期/时间与用户选择的日期/时间进行比较.以下是我的代码段
-(IBAction)btnSaveTouched:(id)sender {
NSDate *today = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd H:mm"];
NSString *formatterDate = [formatter stringFromDate: today];
NSComparisonResult comparisionResult1 = [self.paramSchedule1StartSQLDateString compare:formatterDate];
if (comparisionResult1 == NSOrderedAscending) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Viewing schedule must be after current time." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
NSLog(@"comparisionResult1 %d", comparisionResult1);
}
Run Code Online (Sandbox Code Playgroud)
点击btnSaveTouched(UIButton)后,日期/时间将存储到数据库中并返回到屏幕.(查看用户选择日期/时间的控制器)
但是,当我尝试比较另一个日期/时间时,即使我选择的日期/时间晚于当前日期/时间,也会显示警报.
在NSLog之后的comparsionResult1,值为1,第二次检查总是.我试着做这个NSComparsionResult comparisionResult1 = 0; 但它不能正常工作.有什么办法可以做到这一点吗?
请指教.谢谢