Question

I have a date component say birthdayComponent (it has month and day in it) and I want to check if it lies between to other components.. say (beginDaYComponent and endDayComponent).

I want to check if my birthday component lies between these two dates..? How can I achieve this as I also have to consider the boundary cases say 29th Feb and I will have the endDayCompnents values as 3 for Month and 1 for Day. You can have a look at the code below for more info.

 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];

}

UPDATE:

I want to omit out the YEAR component for the date.

Était-ce utile?

La solution

Create the two NSDate objects that you want to use as the upper and lower bound.

Then use NSDate's compare method to see whether your date is between the two dates. You need to use the compare method twice. Check out compare method's documentation:

  • The receiver and anotherDate are exactly equal to each other, NSOrderedSame
  • The receiver is later in time than anotherDate, NSOrderedDescending
  • The receiver is earlier in time than anotherDate, NSOrderedAscending.

Autres conseils

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
}

i think it's pretty straightforward from here :)

Pretty straightforward. You can use laterDate: method of NSDate or both earlierDate: and laterDate:. Please read description about these methods.

// 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.
}
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];

This will create a start date for time 0:00 and an endate of 23:59:59.999 in the default timezone

Now use the compare method

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.
}

it became apparent, that you want to check it for any year. Just set the yer to a year - like 1 - for the comparison for all involved 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.
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top