11

I would like to write a fuzzy date method for calculating dates in Objective-C for iPhone. There is a popular explanation here:

Calculate relative time in C#

However it contains missing arguments. How could this be used in Objective-C?. Thanks.

const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;

if (delta < 1 * MINUTE)
{
  return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
}
if (delta < 2 * MINUTE)
{
  return "a minute ago";
}
if (delta < 45 * MINUTE)
{
  return ts.Minutes + " minutes ago";
}
if (delta < 90 * MINUTE)
{
  return "an hour ago";
}
if (delta < 24 * HOUR)
{
  return ts.Hours + " hours ago";
}
if (delta < 48 * HOUR)
{
  return "yesterday";
}
if (delta < 30 * DAY)
{
  return ts.Days + " days ago";
}
if (delta < 12 * MONTH)
{
  int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
  return months <= 1 ? "one month ago" : months + " months ago";
}
else
{
  int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
  return years <= 1 ? "one year ago" : years + " years ago";
}
Community
  • 1
  • 1
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144

4 Answers4

23

Dates are represented in Cocoa using the NSDate class. There is a convenient method implemented in NSDate to obtain the delta in seconds between two date instances, timeIntervalSinceDate:. This is called upon an NSDate instance, taking another NSDate object as an argument. It returns an NSTimeInterval (which is a typedef for a double), which is representative of the number of seconds between the two dates.

Given this, it would be fairly simple to adapt the code you have given above to an Objective-C/Cocoa context. Since the delta calculated by NSDate is given in seconds, given two dates, you could easily adapt the code above:

//Constants
#define SECOND 1
#define MINUTE (60 * SECOND)
#define HOUR (60 * MINUTE)
#define DAY (24 * HOUR)
#define MONTH (30 * DAY)

- (NSString*)timeIntervalWithStartDate:(NSDate*)d1 withEndDate:(NSDate*)d2
{
    //Calculate the delta in seconds between the two dates
    NSTimeInterval delta = [d2 timeIntervalSinceDate:d1];

    if (delta < 1 * MINUTE)
    {
        return delta == 1 ? @"one second ago" : [NSString stringWithFormat:@"%d seconds ago", (int)delta];
    }
    if (delta < 2 * MINUTE)
    {
        return @"a minute ago";
    }
    if (delta < 45 * MINUTE)
    {
        int minutes = floor((double)delta/MINUTE);
        return [NSString stringWithFormat:@"%d minutes ago", minutes];
    }
    if (delta < 90 * MINUTE)
    {
        return @"an hour ago";
    }
    if (delta < 24 * HOUR)
    {
        int hours = floor((double)delta/HOUR);
        return [NSString stringWithFormat:@"%d hours ago", hours];
    }
    if (delta < 48 * HOUR)
    {
        return @"yesterday";
    }
    if (delta < 30 * DAY)
    {
        int days = floor((double)delta/DAY);
        return [NSString stringWithFormat:@"%d days ago", days];
    }
    if (delta < 12 * MONTH)
    {
        int months = floor((double)delta/MONTH);
        return months <= 1 ? @"one month ago" : [NSString stringWithFormat:@"%d months ago", months];
    }
    else
    {
        int years = floor((double)delta/MONTH/12.0);
        return years <= 1 ? @"one year ago" : [NSString stringWithFormat:@"%d years ago", years];
    }
}

This would then be called, passing the start and end NSDate objects as arguments, and would return an NSString with the time interval.

Alex Rozanski
  • 37,815
  • 10
  • 68
  • 69
  • 3
    The only problem with this implementation is it makes no distinction between 24 hours as a day, and a calendar day. i.e. if I'm comparing 11:00PM and 2:00AM, the difference should be "yesterday" not "3 hours ago" Look into NSCalendar and its companion NSDateComponents class. – retainCount Jun 28 '09 at 12:47
1

You can get the delta between two NSDate objects by using the timeIntervalSinceDate: method. That'll give you the delta in seconds.

From that you can figure out minutes/hours/days/moths/years by dividing by the appropriate amount.

Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
1

As an alternative, you can avoid the error prone calendar arithmetic by relying on the calendar components you can pull from the difference between two dates:

NSDate *nowDate =    [[NSDate alloc] init];
NSDate *targetDate = nil; // some other date here of your choosing, obviously nil isn't going to get you very far

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
                                            fromDate:dateTime
                                              toDate:nowDate options:0];
NSInteger months = [components month];
NSInteger weeks = [components week];
NSInteger days = [components day];
NSInteger hours = [components hour];
NSInteger minutes = [components minute];

The key is the setup of the unit flags - this allows you to set which units of time you want the date/time to be broken down into. If you just want hours you'd set NSHourCalendarUnit, and that value will just keep on increasing as your dates move further apart, because there isn't a bigger unit to begin incrementing.

Once you have your components, you can proceed with the logic of your choice, perhaps by modifying @alex's conditional flow.

This is what I threw together:

if (months > 1) {
    // Simple date/time
    if (weeks >3) {
        // Almost another month - fuzzy
        months++;
    }
    return [NSString stringWithFormat:@"%ld months ago", (long)months];
}
else if (months == 1) {
    if (weeks > 3) {
        months++;
        // Almost 2 months
        return [NSString stringWithFormat:@"%ld months ago", (long)months];
    }
    // approx 1 month
    return [NSString stringWithFormat:@"1 month ago"];
}
// Weeks
else if (weeks > 1) {
    if (days > 6) {
        // Almost another month - fuzzy
        weeks++;
    }
    return [NSString stringWithFormat:@"%ld weeks ago", (long)weeks];
}
else if (weeks == 1 ||
         days > 6) {
    if (days > 6) {
        weeks++;
        // Almost 2 weeks
        return [NSString stringWithFormat:@"%ld weeks ago", (long)weeks];
    }
    return [NSString stringWithFormat:@"1 week ago"];
}
// Days
else if (days > 1) {
    if (hours > 20) {
        days++;
    }
    return [NSString stringWithFormat:@"%ld days ago", (long)days];
}
else if (days == 1) {
    if (hours > 20) {
        days++;
        return [NSString stringWithFormat:@"%ld days ago", (long)days];
    }
    return [NSString stringWithFormat:@"1 day ago"];
}
// Hours
else if (hours > 1) {
    if (minutes > 50) {
        hours++;
    }
    return [NSString stringWithFormat:@"%ld hours ago", (long)hours];
}
else if (hours == 1) {
    if (minutes > 50) {
        hours++;
        return [NSString stringWithFormat:@"%ld hours ago", (long)hours];
    }
    return [NSString stringWithFormat:@"1 hour ago"];
}
// Minutes
else if (minutes > 1) {
    return [NSString stringWithFormat:@"%ld minutes ago", (long)minutes];
}
else if (minutes == 1) {
    return [NSString stringWithFormat:@"1 minute ago"];
}
else if (minutes < 1) {
    return [NSString stringWithFormat:@"Just now"];
}
bdalziel
  • 2,005
  • 3
  • 17
  • 32