Reputation: 2809
I have the following method which helps me determine if the current day is 3 days or less from the end of the month:
- (BOOL) checkMonthEnd {
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *startOfToday;
[cal rangeOfUnit:NSCalendarUnitDay startDate:&startOfToday interval:NULL forDate:now];
NSDate *startOfNextMonth = ({
NSDate *startOfThisMonth;
NSTimeInterval lengthOfThisMonth;
[cal rangeOfUnit:NSCalendarUnitMonth startDate:&startOfThisMonth interval:&lengthOfThisMonth forDate:now];
[startOfThisMonth dateByAddingTimeInterval:lengthOfThisMonth];
});
NSDateComponents *comp = [cal components:NSCalendarUnitDay fromDate:startOfToday toDate:startOfNextMonth options:0];
if (comp.day < 4) {
return YES;
} else {
return NO;
}
}
What I would like to do is now modify it so that it shows me if the current date is within the first three days of the BEGINNING of the current month. How can I do this?
Upvotes: 1
Views: 320
Reputation: 437702
To determine if it's one of the first three days of the month, just retrieve NSCalendarUnitDay
and see if it's less than 4:
- (BOOL) checkMonthStart {
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSCalendarUnitDay fromDate:now];
return components.day < 4;
}
By the way, if checking whether you're within three days of the end of the month, I might suggest:
- (BOOL) checkMonthEnd {
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSCalendarUnitDay fromDate:now];
NSRange range = [cal rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:now];
return (range.length - components.day) < 3;
}
Upvotes: 4