Settings in the app generally have such settings, such as the night do Not Disturb mode, from 8:00-23:00, at this point how to determine whether the current time is within that time period. The main difficulty is how to use NSDate to generate a 8:00 of the time and 23:00 of the time, and then use the current time to compare these two times.
Two ideas are provided below:
Method 1. Use NSDate to generate the current time, and then into a string, take the current year, month, day from the string, and then spell the time, minutes, seconds, and then the concatenation of the string into NSDate, and finally use the current time with their own generation of the two nsdate time point comparison. (This method is stupid, it is not difficult, but it seems to be a little too much food, it does not look very normative)
Method 2. Use Nsdatecomponents, Nscalendar to determine the time of the two fixed nsdate format, and then compare (this method compared loading force, in fact, with the method of spelling string complexity is not much difference, but looks more normative, like the Great God wrote).
/** * @brief determine if the current time is between Fromhour and Tohour. For example, when fromhour=8,tohour=23, that is, to determine whether the current time is between 8:00-23:00*/-(BOOL) Isbetweenfromhour: (Nsinteger) Fromhour tohour: (nsinteger) tohour{nsdate*date8 = [Self getcustomdatewithhour:8]; NSDate*date23 = [Self getcustomdatewithhour: at]; NSDate*currentdate =[NSDate Date]; if([currentdate compare:date8]==nsordereddescending && [currentdate compare:date23]==nsorderedascending) {NSLog (@"the time is between%d:00-%d:00! ", Fromhour, Tohour); returnYES; } returnNO;}/** * @brief generate a point on the day (London time, which can be compared directly to the current time [NSDate Date]) * @param hour such as hour as "8", which is 8:00 (local time)*/-(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];}
2.
Http://stackoverflow.com/questions/12238395/check-if-the-current-time-is-in-between-time-a-and-b
3.
Http://stackoverflow.com/questions/4084341/how-to-calculate-time-in-hours-between-two-dates-in-ios
3.
Http://stackoverflow.com/questions/13965921/ios-check-whether-current-time-is-between-two-times-or-not
Whether the current time of the IOS break is within a certain time of day.