Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have a few nifty methods on NSDate. This is self-explanatory:</p> <pre><code>-(BOOL)isOnTheSameDayAsDate:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *selfComponents = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:self]; NSDateComponents *dateComponents = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date]; return (([selfComponents day] == [dateComponents day]) &amp;&amp; ([selfComponents month] == [dateComponents month]) &amp;&amp; ([selfComponents year] == [dateComponents year])); } </code></pre> <p>Usage: </p> <pre><code>if ([aDate isOnTheSameDayAsDate:anotherDate]) { ... } </code></pre> <p>This provides a method to easily get dates like "9am the day before": </p> <pre><code>-(NSDate *)dateWithDayDelta:(NSInteger)daysBeforeOrAfter atHour:(NSUInteger)hour minute:(NSUInteger)minute second:(NSUInteger)second { NSDate *date = [self addTimeInterval:(24 * 60 * 60) * daysBeforeOrAfter]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:date]; [comps setHour:hour]; [comps setMinute:minute]; [comps setSecond:second]; return [calendar dateFromComponents:comps]; } </code></pre> <p>Usage: </p> <pre><code>// We want 9am yesterday NSDate *nineAmYesterday = [[NSDate date] dateWithDayDelta:-1 atHour:9 minute:0 second:0]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload