month snippets

Time ago in words (minutes, hours, days, weeks, months ago in words)

Tagged month, week, hour, minutes, words, rails, ruby  Languages ruby
def minutes_in_words(timestamp)
    minutes = ((( - timestamp).abs)/60).round
    return nil if minutes < 0
    case minutes
      when 0..4            then '&lt; 5 minutes'
      when 5..14           then '&lt; 15 minutes'
      when 15..29          then '&lt; 30 minutes'
      when 30..59          then '&gt; 30 minutes'
      when 60..119         then '&gt; 1 hour'
      when 120..239        then '&gt; 2 hours'
      when 240..479        then '&gt; 4 hours'
      when 480..719        then '&gt; 8 hours'
      when 720..1439       then '&gt; 12 hours'
      when 1440..11519     then '&gt; ' << pluralize((minutes/1440).floor, 'day')
      when 11520..43199    then '&gt; ' << pluralize((minutes/11520).floor, 'week')
      when 43200..525599   then '&gt; ' << pluralize((minutes/43200).floor, 'month')  
      else                      '&gt; ' << pluralize((minutes/525600).floor, 'year')

Or use Rails' time_ago_in_words:

How to extract current year, month, and day from an NSDate with NSCalendar, NSDateComponents, and NSDateFormatter

Tagged year, month, day, nsdateformatter, nsdate, nscalendar, nsdatecomponents, iphone  Languages objectivec

You have three options to achieve what the title says:

Using NSDate and NSCalendar to extract current year, month, and day

NSDate *now = [NSDate date];
// Specify which units we would like to use
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:units fromDate:now];

NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];

Using NSDate descriptionWithCalendarFormat to extract current year, month, and day

NSString *year = [now descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil];
NSString *month = [now descriptionWithCalendarFormat:@"%m" timeZone:nil locale:nil];
NSString *day = [now descriptionWithCalendarFormat:@"%d" timeZone:nil locale:nil];

The code will compile but spit out the following warning, because it's not supported on the iPhone:

warning: 'NSDate' may not respond to '-descriptionWithCalendarFormat:timeZone:locale:

Using NSDateFormatter to extract current year, month, and day

NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSString *year = [formatter stringFromDate:now];
[formatter setDateFormat:@"MM"];
NSString *month = [formatter stringFromDate:now];
[formatter setDateFormat:@"dd"];
NSString *day = [formatter stringFromDate:now];