ios - 日期无法比较

标签 ios objective-c nsdate nsdateformatter

我想检查用户选择的日期是否与当前日期相同。我正在使用这段代码。我可以检查所选日期是过去还是将来,但我无法检测它是否相同,因为当我记录这两个日期时,它都是这种格式。我的意思是今天的日期时间和选定的日期时间之间存在差异。所以被认为是较少。这是日志:

selected date:2014-01-03 05:53:53 +0000 , today date: 2014-01-03 05:54:20 +0000

这是我的代码:

 NSDate *today = [NSDate date]; // it will give you current date
NSDate *newDate = datePicker.date; // your date

NSComparisonResult result;
//  has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
NSLog(@"selected date:%@ , today date: %@",newDate,today);
result = [today compare:newDate];
NSLog(@"result %d",result);
if(result==NSOrderedAscending)
{
    NSLog(@"big ascending");
    [AJNotificationView showNoticeInView:[[UIApplication sharedApplication] delegate].window
                                    type:AJNotificationTypeRed
                                   title:@"Birthdate should be less than today date."
                         linedBackground:AJLinedBackgroundTypeDisabled
                               hideAfter:GZAJNotificationDelay];
    return;

}
else if(result==NSOrderedDescending){
    NSLog(@"fine");
    [UIView animateWithDuration:0.25 delay:0.0 options: UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         // self.view.frame=CGRectMake(0, -30, 320, 200);
                         subview.frame=CGRectMake(0, 640, 320, 180);
                         //                             CGRect f1;
                         //                             f1=self.view.frame;
                         //                             f1.origin.y=f1.origin.y-100;
                         // self.view.frame=f1;
                         // v1.backgroundColor=[UIColor blackColor];
                     }
                     completion:^(BOOL finished){
                         if(finished)  {NSLog(@"Finished end !!!!!");}
                     }];
}

else{
    NSLog(@"same");
    [AJNotificationView showNoticeInView:[[UIApplication sharedApplication] delegate].window
                                    type:AJNotificationTypeRed
                                   title:@"Birthdate should be less than today date."
                         linedBackground:AJLinedBackgroundTypeDisabled
                               hideAfter:GZAJNotificationDelay];
    return;
}

最佳答案

试试这个-

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"MM-dd-yyyy"];
NSDate *date1 = [dateFormat dateFromString:firstDate];
NSDate *date2 = [dateFormat dateFromString:secondDate];

NSComparisonResult comparisonResult = [date1 compare:date2];

if(comparisonResult==NSOrderedAscending)
{
}
else if(comparisonResult ==NSOrderedDescending)
{
}
else
{
    // dates are same
}

关于ios - 日期无法比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20897478/

相关文章:

iphone - 如果我在 ios 5 或更高版本上运行,我如何在运行时进行测试?

ios - 导致错误线程 1 :Signal Sigabrt 的 IB 导出和操作

iphone - Objective-C NSMutableArray 在枚举时发生了变异?

php - 如何使用 **TIME ZONE** 将 NSDate 存储到 DATETIME?

objective-c - 将 ISO 8601 转换为 NSDate

cocoa-touch - 计算iOS中给定时间点之前的时间

ios - Xcode8 苹果推送通知服务有问题

ios - 具有多行 UILabel 的自定义 Tableview 单元格需要动态高度

ios - 如何使用 presentViewController 与刚刚显示的 View 进行交互?

iphone - 如何在 UITableView Cell 中制作透明的 UIButton