Reliable check whether NSDate falls in the set hour, day or week

I need to take the saved NSDate and reliably determine if it is currently being hit, hour, week or week. I seem to have cracked the solution, but not having solved this problem before, I am not quite sure that it is reliable.

Will it withstand custom 12 and 24 hour time? the date formatting guide indicates that this user parameter may lead to some unexpected date behavior: โ€œIn iOS, the user can override the default AM / PM compared to the 24-hour time setting. This may cause the NSDateFormatter to overwrite the specified format string .

What about the basic code scheme for this problem? Is this code a reliable way to serve its purpose? I do not like to post the question โ€œcheck my codeโ€, but this is a rather minor problem for me, and complicated enough to strictly verify that it seemed justified. NSDateFormatter is also relatively new to me; different motivation for the question.

NOTE. The main source of my nervousness is that converting dates to strings and then comparing strings seems to be an inherently fragile method of solving this problem. But this is the best I could think of.

Quick link: dateFormat I used for each of three cases:

 dateFormat = @"yyyyMMddHH"; // For "this hour" check dateFormat = @"yyyyMMdd"; // For "today" check dateFormat = @"yyyyww"; // For "this week" check 

Thanks! Code:

 - (BOOL)didThisCycle { // Case 1: hourly; Case 2: daily; Case 3: weekly BOOL did = NO; NSDate *now = [NSDate date]; NSDate *lastDid = [self.didDates lastObject]; if (![lastDid isKindOfClass:[NSDate class]]) { // Crash protection return NO; } int type = [self.goalType intValue]; switch (type) { case 1: { // If hourly check hour NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; formatter.dateFormat = @"yyyyMMddHH"; NSString *nowString = [formatter stringFromDate:now]; NSString *lastDidString = [formatter stringFromDate:lastDid]; if ([nowString isEqualToString:lastDidString]) { did = YES; } else { did = NO; } break; } case 2: { // If daily check day NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; formatter.dateFormat = @"yyyyMMdd"; NSString *nowString = [formatter stringFromDate:now]; NSString *lastDidString = [formatter stringFromDate:lastDid]; if ([nowString isEqualToString:lastDidString]) { did = YES; } else { did = NO; } break; } case 3: { // If weekly check week NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; formatter.dateFormat = @"yyyyww"; NSString *nowString = [formatter stringFromDate:now]; NSString *lastDidString = [formatter stringFromDate:lastDid]; if ([nowString isEqualToString:lastDidString]) { did = YES; } else { did = NO; } break; } default: { did = NO; break; } } return did; } 
+4
source share
1 answer

Use the NSDateComponents class, for example:

 NSDate *someDate = // whatever NSDate *now = [NSDate date]; NSDateComponents *thenComponents = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:someDate]; NSDateComponents *nowComponents = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:now]; if([thenComponents year] == [nowComponents year] && [thenComponents month] == [nowComponents month] && [thenComponents day] == [nowComponents day] && [thenComponents hour] == [nowComponents hour]) { // hooray } 

Remove the hour component if you just want to check the day, or delete both this and the day (and replace it with NSWeekCalendarUnit and the -week method) to check the week.

+7
source

Source: https://habr.com/ru/post/1440880/


All Articles