如何检查日期(来自日期选择器)是否在两个其他日期之间? Objective-C的 [英] How to check if date (from date picker) is between two other dates ? objective-c

查看:151
本文介绍了如何检查日期(来自日期选择器)是否在两个其他日期之间? Objective-C的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从UIDatePicker(MM-dd格式)获取日期并检查它是否在其他两个日期的范围内;

i'm trying to get date from UIDatePicker (MM-dd format) and to check if its in the range of two other dates;

我试过了在很多不同的方式,我认为我的思想已经走得很远:)
有人可以帮助我做到这么简单吗?

i have tried it in so many different ways and i think my mind has gone to far :) can someone help me do it as simple as it should be?

这是我上次尝试的代码:

this is my last try code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM-dd"];
NSString *theDate = [dateFormat stringFromDate:birthdateSelector.date];
NSDate *birthdate = [dateFormat dateFromString:theDate];

NSString *Capricorn_Start = @"12-22" ;
NSString *Capricorn_End = @"01-19" ;

if ([birthdate laterDate:[dateFormat dateFromString:Capricorn_End]] && [birthdate earlierDate:[dateFormat dateFromString:Capricorn_Start]]){

    NSLog(@"im capricorn");
}

谢谢,
Amir。

Thanks, Amir.

推荐答案

如果你有 dateA dateB dateC ,你想要的检查 dateA < dateB < dateC ,您可以获得 dateA-dateB dateB-dateC 之间的区别,它将为您提供 diffX diffY 。如果 diffX 符号等于 diffY 符号,则表示 dateB 位于 A C之间

If you have dateA, dateB and dateC, and you want to check if dateA < dateB < dateC, you can get the difference between dateA-dateB and dateB-dateC which will give you diffX and diffY. If diffX sign is equal than diffY sign, it will mean that dateB is between A and C.

或者换句话说,您的代码如下所示:

Or, in other words, your code would look like this:

NSTimeInterval diffX = [dateB timeIntervalSinceDate:dateA];
NSTimeInterval diffY = [dateC timeIntervalSinceDate:dateB];

if (signbit(diffX) == signbit(diffY))
{
    // dateB is in-between dateA and dateC
}

这篇关于如何检查日期(来自日期选择器)是否在两个其他日期之间? Objective-C的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆