Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are methods from Cocoa to help you to get relevant info (not sure if they are all available in coca-touch).</p> <pre><code> NSDate * today = [NSDate date]; NSLog(@"today: %@", today); NSString * str = @"Thu, 21 May 09 19:10:09 -0700"; NSDate * past = [NSDate dateWithNaturalLanguageString:str locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]; NSLog(@"str: %@", str); NSLog(@"past: %@", past); NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *components = [gregorian components:unitFlags fromDate:past toDate:today options:0]; NSLog(@"months: %d", [components month]); NSLog(@"days: %d", [components day]); NSLog(@"hours: %d", [components hour]); NSLog(@"seconds: %d", [components second]); </code></pre> <p>The NSDateComponents object seems to hold the difference in relevant units (as specified). If you specify all units you can then use this method:</p> <pre><code>void dump(NSDateComponents * t) { if ([t year]) NSLog(@"%d years ago", [t year]); else if ([t month]) NSLog(@"%d months ago", [t month]); else if ([t day]) NSLog(@"%d days ago", [t day]); else if ([t minute]) NSLog(@"%d minutes ago", [t minute]); else if ([t second]) NSLog(@"%d seconds ago", [t second]); } </code></pre> <p>If you want to calculate yourself you can have a look at:</p> <pre><code>NSDate timeIntervalSinceDate </code></pre> <p>And then use seconds in the algorithm.</p> <p><strong>Disclaimer</strong>: If this interface is getting deprecated (I haven't checked), Apple's preferred way of doing this via <code>NSDateFormatters</code>, as suggested in comments below, looks pretty neat as well - I'll keep my answer for historical reasons, it may still be useful for some to look at the logic used.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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