In ios, NSDateComponents, NSDate, and NSCalendar are used to determine whether the current time is within a certain period of one day ., Nscalendarnsdate

Source: Internet
Author: User

In ios, NSDateComponents, NSDate, and NSCalendar are used to determine whether the current time is within a certain period of one day ., Nscalendarnsdate

In the application, this setting usually exists. For example, if you do not disturb at night, you can determine whether the current time is within this period from. The main difficulty lies in how to use NSDate to generate a time and a time, and then compare it with the current time.

The following two ideas are provided:

Method 1. use NSDate to generate the current time, convert it to a string, extract the current year, month, and day from the string, and then combine the time, minute, and second, then convert the spliced string to NSDate, and compare the current time with the time point of the two generated NSDate. (This method is stupid and not difficult, but it looks a little too cheesy)

Method 2. use NSDateComponents and NSCalendar to determine the time of the two fixed NSDate formats, and then compare them. (This method is relatively difficult. In fact, it is not much less complex than the String concatenation method, but it looks more standard, as written by a great God ).

/*** @ Brief determines whether the current time is between fromHour and toHour. For example, fromHour = 8 and toHour = 23 are used to determine whether the current time is between-*/-(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 (@ "this time is between % d: 00-% d: 00! ", FromHour, toHour); return YES;} return NO;}/*** @ brief generates a point on the current day (return the London time, can be directly compared with the current time [NSDate date]) * @ param hour if hour is "8", that is, am (local time) */-(NSDate *) getCustomDateWithHour :( NSInteger) hour {// obtain the current time NSDate * currentDate = [NSDate date]; NSCalendar * currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDateComponents * currentComps = [[NSDateComponents alloc] init]; NSInteger unitFlags = Alibaba | NSDayCalendarUnit | Alibaba | NSSecondCalendarUnit; currentComps = [currentCalendar components: unitFlags fromDate: currentDate]; // set a certain 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] calendar: NSGregorianCalendar]; return [resultCalendar dateFromComponents: resultComps];}



How to use [NSDate date] to distinguish between day and night?

NSDate * now = [NSDate date]; NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar ar] autoreler]; NSDateComponents * components = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate: now]; if (components. hour> 12) else should be correct during the day at night

Why is there a one-second error in NSDateComponents computing time difference?

[NSDate now] Is there this method? What is addDay function?
I have written similar code here:
NSDate * date1 = [NSDate dateWithTimeIntervalSince1970: 0];
NSDate * date2 = [NSDate dateWithTimeIntervalSince1970: 48*3600];
NSCalendar * gregorian = [NSCalendar currentCalendar];
NSUInteger unilags = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents * components = [gregorian components: unilags fromDate: date1 toDate: date2 options: 0];
NSInteger months = [components month];
NSInteger days = [components day];
NSInteger hours = [components hour];
NSInteger minutes = [components minute];
NSInteger seconds = [components second];
NSLog (@ "% d -- % d", months, days, hours, minutes, seconds );
The output is 0--2--0--0--0, and there should be no errors. It may be the internal reason of the custom function! If it does not involve content leaks, you can send the code to me to see, xpsuperman@126.com
 

Related Article

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.