0

Possible Duplicate:
Fuzzy Date algorithm in Objective-C

I have a NSString parsed from a xml that contains a date such as

Mon May 10 23:26:22 +0000 2010

I am having a difficult time converting that NSString "date" to something more efficient, such as

5hrs ago

Yesterday

a Week ago

If anyone can point me in the right direction as how to attempt this it would be greatly appreciated.

Community
  • 1
  • 1
FreeAppl3
  • 858
  • 1
  • 15
  • 32

2 Answers2

1

I know exactly what you mean. Try this, I have been using this & it works perfectly for me -

Usage - [Utils humanDates:timeSinceEpoch];

+ (NSString *)humanDates:(NSString *)origDateInEpochTime
{
    if([origDateInEpochTime length]<= 0)
        return @"never";

    NSDate *convertedDate = [NSDate dateWithTimeIntervalSince1970:[origDateInEpochTime doubleValue]/1000];
    NSDate *todayDate     = [NSDate date];
    double ti             = [convertedDate timeIntervalSinceDate:todayDate];
    ti = ti * -1;
    if(ti < 1)
    {
        return @"never";
    }
    else if(ti < 60)
    {
        return @"less than a minute ago";
    }
    else if(ti < 3600)
    {
        int diff = round(ti/60);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d minute ago", diff];
        else
            return [NSString stringWithFormat:@"%d minutes ago", diff];
    }
    else if(ti < 86400)
    {
        int diff = round(ti/60/60);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d hour ago", diff];
        else
            return [NSString stringWithFormat:@"%d hours ago", diff];
    }
    else if(ti < 2629743)
    {
        int diff = round(ti/60/60/24);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d day ago", diff];
        else
            return [NSString stringWithFormat:@"%d days ago", diff];
    }
    else if(ti < 31104000)
    {
        int diff = round(ti/60/60/24/30);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d month ago", diff];
        else
            return [NSString stringWithFormat:@"%d months ago", diff];
    }
    else if(ti < 311040000)
    {
        int diff = round(ti/60/60/24/30/12);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d year ago", diff];
        else
            return [NSString stringWithFormat:@"%d years ago", diff];
    }
    else
    {
        int diff = round(ti/60/60/24/30/12/10);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d decade ago", diff];
        else
            return [NSString stringWithFormat:@"%d decades ago", diff];
    }
}
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
1

i do in this way

first i write down function like this

NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];

[dateForm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *dateSelected = [dateForm dateFromString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:0]]];

NSInteger nDiffDate = [self howManyDaysHavePast:dateSelected today:[NSDate date]];

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

 [dateFormatter setDateFormat:@"EEEE"];

 NSString *dayName = [dateFormatter stringFromDate:dateSelected];

[dateFormatter release];
[dateForm release];

            if (nDiffDate ==0) {
                dateLbl.text   = [[NSString stringWithFormat:@"%@",[recentHisotryArr objectAtIndex:0]]substringFromIndex:11];

            }
            else if(nDiffDate==1)
            {
            dateLbl.text = @"Yesterday";

            }

            else if(nDiffDate==2)
            {

                dateLbl.text = dayName;

            } 
            else if(nDiffDate==3)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==4)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==5)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==6)
            {
                dateLbl.text = dayName;                
            } 

-(int)howManyDaysHavePast:(NSDate*)lastDate today:(NSDate*)today 
{
    NSDate *startDate = lastDate;
    NSDate *endDate = today;
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:startDate
                                                  toDate:endDate options:0];
    int days = [components day];
    return days;
}

and it work for me like the day reminder on the dial app on the iphone

Maulik
  • 19,348
  • 14
  • 82
  • 137
B25Dec
  • 2,301
  • 5
  • 31
  • 54