Shinnyx
Shinnyx

Reputation: 2264

NSDateFormatter - "Yesterday, 3:30 PM"

I want to get a particular format for my date in my iOS app. Basically, it's a text message history list. I want to display the date with the following format if the text was received less than a week ago:

"Today, 3:30 PM"

"Yesterday, 3:28 AM"

"Monday, 1:12 PM"

etc.

How can I use NSDateFormatter to get that output? I've tried few combinations without success, the main problem reside in the first part; Today/yesterday, etc.

Thanks a lot!

EDIT: Here's my code at this moment. (The day part isn't working, I have trouble getting the "dateFromTemplate" & "timeStyle" to work at the same time.)

NSString *dateComponents = @"eee";
formatter=[[NSDateFormatter alloc] init];
formatter.timeStyle=NSDateFormatterShortStyle;
[formatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:[NSLocale autoupdatingCurrentLocale]]];
formatter.doesRelativeDateFormatting=YES;
formatter.locale=[NSLocale autoupdatingCurrentLocale];

Upvotes: 0

Views: 436

Answers (2)

user3575114
user3575114

Reputation: 993

  1. Create a date component and assign a day value to -1 and you can find yesterday's date by adding date component to current date-

    NSDateComponents *NsD = [[NSDateComponents alloc] init];
    NsD.day = -1;
    NSDate *yesterday = [[NSCalendar currentCalendar] dateByAddingComponents:NsD toDate:dateNow  options:0];
    
  2. You can get time by using date formatter

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];        
    [formatter setDateFormat:@"hh:mm a"];
    NSString *dateStr = [formatter stringFromDate:[NSDate date]];
    

Upvotes: 0

Sandeep
Sandeep

Reputation: 21154

It is easy to implement one yourself, if you take a look at NSDateComponents. However, there are some nifty libraries to do this. See this one, it looke good https://github.com/kevinlawler/NSDate-TimeAgo. This is the code I wrote for an application, you could extend it to your need. This is a category on NSDate.

@implementation NSDate(DateAgo)

+ (NSDate*)dateEarlierToDate:(NSDate*)date byDays:(int)days{
  NSCalendar *calendar = [NSCalendar currentCalendar];
  NSDateComponents *components = [[NSDateComponents alloc] init];
  [components setDay:-days];
  return [calendar dateByAddingComponents:components toDate:date options:0];
}


-(NSString*)timeAgoFromReference{
    static NSDateFormatter *formatter;
    if(!formatter){
        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterShortStyle];
    }
    NSDate *now = [NSDate date];
    NSTimeInterval timeIntervalFromNow = [now timeIntervalSinceDate:self];
    if(timeIntervalFromNow/(60*60*24) > 9){
        return [formatter stringFromDate:self];
    }else{
        return [self timeAgoFromInterval:timeIntervalFromNow];
    }

}
-(NSString*)timeAgoFromInterval:(NSTimeInterval)interval{
    if(interval < 1){
        return @"A second ago";
    }else if(interval < 5){
        return @"5 seconds ago";
    }else if(interval < 10){
        return @"10 seconds ago";
    }else if(interval < 15){
        return @"15 seconds ago";
    }else if(interval < 30){
           return  @"30 seconds ago";
    }else if(interval < 60){
        return @"1 minute ago";
    }else if(fabs(interval/60) < 60 ){
        return [NSString stringWithFormat:@"%d minutes ago",(int)(interval/60)];
    }else{
        int timeInHours = interval/(60*60);
        if(timeInHours == 1){
            return @"1 hour ago";
        }else if(timeInHours < 24){
            return [NSString stringWithFormat:@"%d hours ago",timeInHours];
        }else{
            int timeInDay = timeInHours/24;
            if(timeInDay == 1){
                return @"1 day ago";
            }else{
                return [NSString stringWithFormat:@"%d days ago", timeInDay];
            }
        }
    }
    return @"0";
}
@end

If you have some date you could call it like this,

NSString *dateAgo = [date timeAgoFromReference];

This is however simple and easy to understand, you could add more features to this yourself easily

Upvotes: 1

Related Questions