+(BOOL) Isbetweenfromhour: (Nsinteger) Fromhour tohour: (nsinteger) tohour{nsdate*datefromhour =[self getcustomdatewithhour:fromhour]; NSDate*datetohour =[self getcustomdatewithhour:tohour]; NSDate*currentdate =[NSDate Date]; if([currentdate compare:datefromhour]==nsordereddescending && [currentdate compare:datetohour]==nsorderedascending) { returnYES; } returnNO;}+ (NSDate *) Getcustomdatewithhour: (Nsinteger) hour{//Get current TimeNSDate *currentdate =[NSDate Date]; Nscalendar*currentcalendar =[[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar]; Nsdatecomponents*currentcomps =[[Nsdatecomponents alloc] init]; Nsinteger Unitflags= Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nsweekdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit |Nssecondcalendarunit; Currentcomps=[Currentcalendar components:unitflags fromdate:currentdate]; //set a point for the dayNsdatecomponents *resultcomps =[[Nsdatecomponents alloc] init]; [Resultcomps setyear:[currentcomps Year]]; [Resultcomps Setmonth:[currentcomps Month]]; [Resultcomps Setday:[currentcomps Day]; [Resultcomps Sethour:hour]; Nscalendar*resultcalendar =[[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar]; return[Resultcalendar datefromcomponents:resultcomps];}
Determine whether the current time is within a time period