我有一个日期组件说birthdayComponent(它有月和日),我想检查它是否位于其他组件之间..说(beginDaYComponent和endDayComponent)。
我想检查我的生日成分是否介于这两个日期之间..?我怎样才能实现这个目标,因为我还必须考虑2月29日的边界情况,并且我将endDayCompnents值设为3个月和1个日。您可以查看下面的代码以获取更多信息。
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
NSDateComponents *beginDayComponent = [[NSDateComponents alloc] init];
NSDateComponents *endDayComponent = [[NSDateComponents alloc] init];
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSMutableArray* birthdayFitlerArray = [NSMutableArray new];
for (NSString* dateString in birthdaysArray)
{
NSDate* birthdayDate = [dateFormat dateFromString:dateString];
dayComponent.day = -1;
NSDate* beginDate = [theCalendar dateByAddingComponents:dayComponent toDate:currentLeg.date options:0];
dayComponent.day = 1;
NSDate* endDate = [theCalendar dateByAddingComponents:dayComponent toDate:currentLeg.date options:0];
beginDayComponent = [theCalendar components:(NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:beginDate];
endDayComponent = [theCalendar components:(NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:endDate];
NSDateComponents* birthdayComponents = [theCalendar components:(NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:birthdayDate];
}
更新
我想省略日期的YEAR组件。
答案 0 :(得分:3)
创建要用作上限和下限的两个NSDate对象。
然后使用NSDate的比较方法来查看您的日期是否在两个日期之间。您需要使用compare方法两次。查看compare method's documentation:
- 接收者和另一个日子完全相同,NSOrderedSame
- 接收者的时间晚于anotherDate,NSOrderedDescending
- 接收者的时间早于另一个日期,NSOrderedAscending。
答案 1 :(得分:1)
NSDateComponents *firstDayDateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:randomdate];
[firstDayDateComponents setDay:1]; // set what you want here
[firstDayDateComponents setHour:0];
[firstDayDateComponents setMinute:0];
NSComparisonResult result = [randomdate compare:date_today];
if ( result == NSOrderedAscending) // if today's date is bigger than randomdate)
{
// do stuff here
}
我认为从这里开始非常简单:)
答案 2 :(得分:0)
NSDate *startDate = …;
NSDate *endDate = …; // make sure endDate is later than startDate
NStimeInterVal endInterval;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&startDate interval:NULL forDate:startDate];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&endDate interval:&endInterval forDate:endDate];
endDate = [endDate dateByAddingTimeInterval:endInterval-0.001];
这将在默认时区创建时间0:00的开始日期和23:59:59.999的结束
现在使用比较方法
if ([givenDate compare:startDate] == NSOrderedAscending){
//date is before both dates
} else if ([givenDate compare:endDate] == NSOrderedDescending){
// date lies after both dates
} else {
//givenDate lies between both dates.
}
NSDateComponents *comps = [aCalendar components:(NSUIntegerMax - NSYearCalendarUnit ) fromDate:date];
comps.year = 1;
NSDate *year1Date = [aCalendar dateFromComponents:comps];
NSDate *startDate = …;
NSDate *endDate = …; // make sure endDate is later than startDate
NStimeInterVal endInterval;
NSDateComponents *comps = [aCalendar components:(NSUIntegerMax - NSYearCalendarUnit ) fromDate:startDate];
comps.year = 1;
NSDate *year1StartDate = [aCalendar dateFromComponents:comps];
comps = [aCalendar components:(NSUIntegerMax - NSYearCalendarUnit ) fromDate:endDate];
comps.year = 1;
NSDate *year1EndDate = [aCalendar dateFromComponents:comps];
comps = [aCalendar components:(NSUIntegerMax - NSYearCalendarUnit ) fromDate:givenDate];
comps.year = 1;
NSDate *year1givenDate = [aCalendar dateFromComponents:comps];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&year1StartDate interval:NULL forDate:year1StartDate;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&endDate interval:&endInterval forDate:endDate];
endDate = [endDate dateByAddingTimeInterval:endInterval-0.001];
if ([year1givenDate compare:year1StartDate] == NSOrderedAscending){
//date is before both dates
} else if ([year1givenDate compare:year1EndDate] == NSOrderedDescending){
// date lies after both dates
} else {
//givenDate lies between both dates.
}
答案 3 :(得分:0)
非常简单。您可以使用laterDate:
NSDate
或earlierDate:
和laterDate:
两种方法。请阅读有关这些方法的说明。
// 1.
NSDate *firstDate, *secondDate, *thirdDate;
if ([firstDate laterDate:secondDate] == secondDate &&
[secondDate laterDate:thirdDate] == thirdDate)
{
/// secondDate is between first and third date.
}
// 2.
if ([secondDate earlierDate:firstDate] == firstDate &&
[secondDate laterDate:thirdDate] == thirdDate)
{
/// secondDate is between first and third date.
}