iOS is using nsdatecomponents, NSDate, Nscalendar it concludes that the current time is over a period of time in a day.

Source: Internet
Author: User

General application Settings This group exists, such as night mode, if you. From 8:00-23:00. At this current time is how to infer the period. The main difficulty is how to use NSDate to generate 8:00 time and 23:00 hours. Then use the current time, perhaps there is enough time to make the control.

Here are two ways of thinking:

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 comparison. (This method is more stupid and not difficult.) But it looks a little too vegetable, and it doesn't look very regular.

Method 2. Use Nsdatecomponents, Nscalendar to determine the time of the two fixed nsdate format, and then compare (this method is more loaded, in fact, the method of spelling the string is not much less complex.) But it looks more normative, like the great God wrote.

/** * @brief Infer whether the current time is between Fromhour and Tohour.

Such as. When fromhour=8,tohour=23. That is, to infer 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:23]; NSDate *currentdate = [NSDate Date]; if ([currentdate compare:date8]==nsordereddescending && [currentdate compare:date23]==nsorderedascending) { NSLog (@ "The time is between%d:00-%d:00! ", Fromhour, Tohour); return YES; } return NO; /** * @brief generate a point on the day (London time is returned, which can be compared directly to the current time [NSDate Date]) * @param hour such as hour is "8". is 8:00 (local time) */-(NSDate *) Getcustomdatewithhour: (Nsinteger) hour{//Get current time nsdate *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 of the day nsdatecomponents *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];} PS: There's a better way to do it, and then soon you say, Dabigatran: 172158202



iOS is using nsdatecomponents, NSDate, Nscalendar it concludes that the current time is over a period of time in a day.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.