我有一个日期组件,比如生日组件(其中包含月份和日期),我想检查它是否位于其他组件之间。比如说(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 组件。