- (NSDate *)getCustomDateWithHour:(NSInteger)hour
{
獲取當(dāng)前時(shí)間
NSDate *currentDate = [NSDate date];
NSCalendar*currentCalendar=[[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *currentComps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
NSLog(@"-----------weekday is %zd",[currentComps weekday]);//在這里需要注意的是:星期日是數(shù)字1谤专,星期一時(shí)數(shù)字2,以此類推。杠步。伪嫁。
設(shè)置當(dāng)天的某個(gè)點(diǎn)
NSDateComponents*resultComps=[[NSDateComponentsalloc]init];
[resultComps setYear:[currentComps year]];
[resultComps setMonth:[currentComps month]];
[resultComps setDay:[currentComps day]];
[resultComps setHour:hour];
NSCalendar*resultCalendar=[[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
return [resultCalendar dateFromComponents:resultComps];
}
- (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour
{
NSDate *fromDate = [self getCustomDateWithHour:fromHour];
NSDate *toDate = [self getCustomDateWithHour:toHour];
NSDate *currentDate = [NSDate date];
if ([currentDate compare:fromDate] == NSOrderedDescending && [currentDate compare:toDate] == NSOrderedAscending)
{
NSLog(@"該時(shí)間在 %ld:00-%ld:00 之間枫慷!", (long)fromHour, (long)toHour);
return YES;
}
return NO;
}