Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing NSDates Without Regard To Time
    primarykey
    data
    text
    <p>Essentially I have a tip of the day feature, with specific tips associated with dates, and I need a way to compare the current <code>NSDate</code> to the saved date for the tip, regardless of time. Obviously the tip for January 1st is relevant at 10AM and 10:30, so I can't literally compare the <code>NSDates</code>. Here are some of the things I've tried:</p> <p><strong>Try 1 - Doesn't work</strong></p> <pre><code>NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; NSInteger day = [components day]; NSInteger month = [components month]; NSInteger year = [components year]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; // [df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; </code></pre> <p><strong>Try 2 - Doesn't work</strong></p> <pre><code>NSString * zoneString = [[NSString stringWithFormat:@"%i-%i-%iT00:00:00", year, month, day] substringFromIndex:([[NSString stringWithFormat:@"%i-%i-%iT00:00:00", year, month, day] length] - 5)]; NSTimeInterval timeInterval = [[zoneString substringToIndex:3] intValue] * 3600; timeInterval += [[zoneString substringFromIndex:3] intValue] * 60; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:timeInterval]]; [formatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss ZZZ"]; NSLog(@"%@", [formatter stringFromDate:myDate]); </code></pre> <p><strong>Try 3 - Doesn't work</strong></p> <pre><code>NSDate *date = [NSDate date]; [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&amp;date interval:NULL forDate:date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; if ([[dateFormat stringFromDate:date] isEqualToString:[dateFormat stringFromDate:tipDate]]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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