gpt4 book ai didi

ios - 日期无法比较

转载 作者:行者123 更新时间:2023-11-29 03:22:37 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com