How to convert NSDate in to relative format as "Today","Yesterday","a week ago","a month ago","a year ago"?

32,005

Solution 1

For simplicity I'm assuming that the dates you are formatting are all in the past (no "tomorrow" or "next week"). It's not that it can't be done but it would be more cases to deal with and more strings to return.


You can use components:fromDate:toDate:options: with whatever combination of date components you are looking for to get the number of years, months, weeks, days, hours, etc. between two dates. By then going though them in order from most significant (e.g. year) to least significant (e.g. day), you can format a string based only on the most significant component.

For example: a date that is 1 week, 2 days and 7 hours ago would be formatted as "1 week".

If you want to create special strings for a special number of a unit, like "tomorrow" for "1 day ago" then you can check the value of that component after you have determined that it is the most significant component.

The code would look something like this:

- (NSString *)relativeDateStringForDate:(NSDate *)date
{
    NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitWeekOfYear | 
                           NSCalendarUnitMonth | NSCalendarUnitYear;

    // if `date` is before "now" (i.e. in the past) then the components will be positive
    NSDateComponents *components = [[NSCalendar currentCalendar] components:units
                                                                   fromDate:date
                                                                     toDate:[NSDate date]
                                                                    options:0];

    if (components.year > 0) {
        return [NSString stringWithFormat:@"%ld years ago", (long)components.year];
    } else if (components.month > 0) {
        return [NSString stringWithFormat:@"%ld months ago", (long)components.month];
    } else if (components.weekOfYear > 0) {
        return [NSString stringWithFormat:@"%ld weeks ago", (long)components.weekOfYear];
    } else if (components.day > 0) {
        if (components.day > 1) {
            return [NSString stringWithFormat:@"%ld days ago", (long)components.day];
        } else {
            return @"Yesterday";
        }
    } else {
        return @"Today";
    }
}

If your dates could also be in the future then you can check the absolute value of the components in the same order and then check if it's positive or negative to return the appropriate strings. I'me only showing the year below:

if ( abs(components.year > 0) ) { 
    // year is most significant component
    if (components.year > 0) {
        // in the past
        return [NSString stringWithFormat:@"%ld years ago", (long)components.year];
    } else {
        // in the future
        return [NSString stringWithFormat:@"In %ld years", (long)components.year];
    }
} 

Solution 2

Please note that as of iOS 13 there is now RelativeDateTimeFormatter which does it all most of it for you! WWDC 2019 video here.

let formatter = RelativeDateTimeFormatter()
let dateString = formatter.localizedString(for: aDate, relativeTo: now)

// en_US: "2 weeks ago"
// es_ES: "hace 2 semanas"
// zh_TW: "2 週前"

I've left my previous answer below for posterity. Cheers!

⚠️ You will want to read through the previous answer for some key tips to avoid certain bugs. Hint: use the end of the current day's date/time for the relative date when comparing dates that are not today!


Here's my answer (in Swift 3!) and why it's better.

Answer:

func datePhraseRelativeToToday(from date: Date) -> String {

    // Don't use the current date/time. Use the end of the current day 
    // (technically 0h00 the next day). Apple's calculation of 
    // doesRelativeDateFormatting niavely depends on this start date.
    guard let todayEnd = dateEndOfToday() else {
        return ""
    }

    let calendar = Calendar.autoupdatingCurrent

    let units = Set([Calendar.Component.year,
                 Calendar.Component.month,
                 Calendar.Component.weekOfMonth,
                 Calendar.Component.day])

    let difference = calendar.dateComponents(units, from: date, to: todayEnd)

    guard let year = difference.year,
        let month = difference.month,
        let week = difference.weekOfMonth,
        let day = difference.day else {
            return ""
    }

    let timeAgo = NSLocalizedString("%@ ago", comment: "x days ago")

    let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.locale = Locale.autoupdatingCurrent
        formatter.dateStyle = .medium
        formatter.doesRelativeDateFormatting = true
        return formatter
    }()

    if year > 0 {
        // sample output: "Jan 23, 2014"
        return dateFormatter.string(from: date)
    } else if month > 0 {
        let formatter = DateComponentsFormatter()
        formatter.unitsStyle = .brief // sample output: "1mth"
        formatter.allowedUnits = .month
        guard let timePhrase = formatter.string(from: difference) else {
            return ""
        }
        return String(format: timeAgo, timePhrase)
    } else if week > 0 {
        let formatter = DateComponentsFormatter()
        formatter.unitsStyle = .brief; // sample output: "2wks"
        formatter.allowedUnits = .weekOfMonth
        guard let timePhrase = formatter.string(from: difference) else {
            return ""
        }
        return String(format: timeAgo, timePhrase)
    } else if day > 1 {
            let formatter = DateComponentsFormatter()
            formatter.unitsStyle = .abbreviated; // sample output: "3d"
            formatter.allowedUnits = .day
            guard let timePhrase = formatter.string(from: difference) else {
                return ""
            }
            return String(format: timeAgo, timePhrase)
    } else {
        // sample output: "Yesterday" or "Today"
        return dateFormatter.string(from: date)
    }
}

func dateEndOfToday() -> Date? {
    let calendar = Calendar.autoupdatingCurrent
    let now = Date()
    let todayStart = calendar.startOfDay(for: now)
    var components = DateComponents()
    components.day = 1
    let todayEnd = calendar.date(byAdding: components, to: todayStart)
    return todayEnd
}

Remember to reuse your formatters to avoid any performance hit! Hint: extensions on DateFormatter and DateComponentsFormatter are good ideas.

Why it's better:

  • Utilizes DateFormatter's "Yesterday" and "Today". This is already translated by Apple, which saves you work!
  • Uses DateComponentsFormatter's already translated "1 week" string. (Again less work for you, courtesy of Apple.) All you have to do is translate the "%@ ago" string. 🙂
  • The other answers incorrectly calculate the time when the day switches from "today" to "yesterday" to etc. Fixed constants are a big NO-NO because reasons. Also, the other answers use the current date/time when they should use the end of the current day's date/time.
  • Uses autoupdatingCurrent for Calendar & Locale which ensures your app is immediately up to date with the user's calendar and language preferences in Settings.app

This answer was inspired by DateTools on GitHub.

Solution 3

Swift update, thanks to objective-c answer of David Rönnqvist, it will work for the past dates.

func relativeDateStringForDate(date : NSDate) -> NSString {

        let todayDate = NSDate()
        let units: NSCalendarUnit = [.Hour, .Day, .Month, .Year, .WeekOfYear]
        let components = NSCalendar.currentCalendar().components(units, fromDate: date , toDate: todayDate, options: NSCalendarOptions.MatchFirst )

        let year =  components.year
        let month = components.month
        let day = components.day
        let hour = components.hour
        let weeks = components.weekOfYear
        // if `date` is before "now" (i.e. in the past) then the components will be positive

        if components.year > 0 {
            return NSString.init(format: "%d years ago", year);
        } else if components.month > 0 {
            return NSString.init(format: "%d months ago", month);
        } else if components.weekOfYear > 0 {
            return NSString.init(format: "%d weeks ago", weeks);
        } else if (components.day > 0) {
            if components.day > 1 {
                return NSString.init(format: "%d days ago", day);
            } else {
                return "Yesterday";
            }
        } else {
            return NSString.init(format: "%d hours ago", hour);
        }
    }

Solution 4

FOR: SWIFT 3

Here's a Swift 3 version, for past dates, that handles all units and singular or plural in the returned String.

Example Use:

let oneWeekAgo = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: Date())!

print(relativePast(for: oneWeekAgo)) // output: "1 week ago"

I based it on a riff off of Saurabh Yadav's. Thanks.

func relativePast(for date : Date) -> String {

    let units = Set<Calendar.Component>([.year, .month, .day, .hour, .minute, .second, .weekOfYear])
    let components = Calendar.current.dateComponents(units, from: date, to: Date())

    if components.year! > 0 {
        return "\(components.year!) " + (components.year! > 1 ? "years ago" : "year ago")

    } else if components.month! > 0 {
        return "\(components.month!) " + (components.month! > 1 ? "months ago" : "month ago")

    } else if components.weekOfYear! > 0 {
        return "\(components.weekOfYear!) " + (components.weekOfYear! > 1 ? "weeks ago" : "week ago")

    } else if (components.day! > 0) {
        return (components.day! > 1 ? "\(components.day!) days ago" : "Yesterday")

    } else if components.hour! > 0 {
        return "\(components.hour!) " + (components.hour! > 1 ? "hours ago" : "hour ago")

    } else if components.minute! > 0 {
        return "\(components.minute!) " + (components.minute! > 1 ? "minutes ago" : "minute ago")

    } else {
        return "\(components.second!) " + (components.second! > 1 ? "seconds ago" : "second ago")
    }
}

Solution 5

To avoid the 24-hour problem mentioned by Budidino to David's answer, I altered it to like this below -

- (NSString *)relativeDateStringForDate:(NSDate *)date
{

NSCalendarUnit units = NSDayCalendarUnit | NSWeekOfYearCalendarUnit |
NSMonthCalendarUnit | NSYearCalendarUnit ;
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components1 = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components1];

components1 = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:date];
NSDate *thatdate = [cal dateFromComponents:components1];

// if `date` is before "now" (i.e. in the past) then the components will be positive
NSDateComponents *components = [[NSCalendar currentCalendar] components:units
                                                               fromDate:thatdate
                                                                 toDate:today
                                                                options:0];

if (components.year > 0) {
    return [NSString stringWithFormat:@"%ld years ago", (long)components.year];
} else if (components.month > 0) {
    return [NSString stringWithFormat:@"%ld months ago", (long)components.month];
} else if (components.weekOfYear > 0) {
    return [NSString stringWithFormat:@"%ld weeks ago", (long)components.weekOfYear];
} else if (components.day > 0) {
    if (components.day > 1) {
        return [NSString stringWithFormat:@"%ld days ago", (long)components.day];
    } else {
        return @"Yesterday";
    }
} else {
    return @"Today";
}
}

Basically, it creates 2 new dates without time pieces included.Then the comparison is done for "days" difference.

Share:
32,005
User 1531343
Author by

User 1531343

Updated on November 09, 2020

Comments

  • User 1531343
    User 1531343 over 3 years

    I want to convert nsdate in to relative format like "Today","Yesterday","a week ago","a month ago","a year ago","date as it is".

    I have written following method for it.. but some how its just printing as it is date.. can you please tell me what should be the problem?

    //Following is my function which converts the date into relative string

    +(NSString *)getDateDiffrence:(NSDate *)strDate{
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
    
        df.timeStyle = NSDateFormatterMediumStyle;
        df.dateStyle = NSDateFormatterShortStyle;
        df.doesRelativeDateFormatting = YES;
        NSLog(@"STRING DATEEE : %@ REAL DATE TODAY %@",[df stringFromDate:strDate],[NSDate date]);
          return [df stringFromDate:strDate];
    
    }
    

    I have date string with the following format "2013-10-29T09:38:00"

    When I tried to give the NSDate object then its always return me null date.
    so I tried to convert that date in to yyyy-MM-dd HH:mm:ssZZZZ then I pass this date to function then it's just printing whole date..

    How to solve this problem?

    //Following is the code I call the above function

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    NSDate *date = [formatter dateFromString:[threadDict objectForKey:@"lastMessageDate"]];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ssZZZZ"];
    
    NSString *date1 = [formatter stringFromDate:date];
    NSDate *date_d = [formatter dateFromString:date1];
    NSString *resultstr=[UserManager getDateDiffrence:date];
    
    self.dateLabel.text=resultstr;
    
  • Jasper Blues
    Jasper Blues over 10 years
    To help get started, here's some code for fist and late day of week: stackoverflow.com/questions/1106943/…
  • David Rönnqvist
    David Rönnqvist over 10 years
    -1 for relying on a day being 24 hours. +1 for at least pointing it out.
  • David Rönnqvist
    David Rönnqvist over 10 years
    @AnoopVaidya I started from the top and down voted when I saw const NSTimeInterval secondsPerDay = 60 * 60 * 24 but when I got to "It is naive for a number of reasons: [...]" I reverted it :)
  • TOMKA
    TOMKA over 10 years
    You shouldn't use %zd for the various properties of NSDateComponents because the properties of NSDateComponents use NSInteger, not size_t. The String Programming Guide recommends using %ld and casting to long. On 64-bit platforms the cast will be a no-op.
  • n00bProgrammer
    n00bProgrammer over 10 years
    Is there a way to get the exact day using NSDateComponents? Like Tuesday, Sunday, etc.
  • David Rönnqvist
    David Rönnqvist over 10 years
    @n00bProgrammer You can get the weekDay component (e.g. 1-7) and use it as the index for the localized weekdaySymbols from a NSDateFormatter
  • budiDino
    budiDino almost 10 years
    It's only good to for the time difference but today/yesterday doesn't work because everything is considered as today if it's within last 24 hours :/
  • mahboudz
    mahboudz over 9 years
    If it is 9am current time, your code will label all the time from 9am yesterday through 9am now, as being "Today". 'yesterday' is considered anytime after 9 hours ago. In other words, you need to compare the date to the last midnight not the current time.
  • n00bProgrammer
    n00bProgrammer over 9 years
    You are right. I had realised this, and used NSDateComponents. I used the components: fromDate: toDate: options: from NSCalendar for accurate time differences. If you need code for this, I'd be happy to provide it.
  • Ludovic Landry
    Ludovic Landry over 9 years
    This is wrong, you cannot assume there is always 86400 sec in a day for example.
  • iosCurator
    iosCurator over 9 years
    @n00bProgrammer I also have the same issue. Can you share the code snippet.
  • gnasher729
    gnasher729 over 9 years
    This is absolutely awful. I'm typing this at 8:50 am. Your code would say that yesterday 8:51 am was "today". Anyone following your advice will take time to unlearn it and do things properly.
  • garanda
    garanda about 8 years
    You have a little mistake on the future dates days from now, it should be less than -1 e.g: if (components.day < -1) { return [NSString stringWithFormat:@"%ld days from now", labs((long)components.day)]; }
  • user2067021
    user2067021 almost 8 years
    NSDate-TimeAgo has been superseded by DateTools. Just use [date timeAgoSinceNow] or [date timeAgoSinceDate:otherDate].
  • ChrisJF
    ChrisJF over 7 years
    CalendarUnit should be used, not the above dictionary.
  • Jesse Onolemen
    Jesse Onolemen almost 7 years
    it would be good to add something like 'just now' for the past 3 seconds
  • William Hu
    William Hu over 6 years
    If second is 0, then the last return will be 0 Just Now
  • Politta
    Politta over 5 years
    It's missing a parentheses in the last return.
  • Shamsiddin Saidov
    Shamsiddin Saidov about 4 years
    @budidino do you have any solution to this problem?
  • grep
    grep over 2 years
    Is there a way to do backwards formatting, i.e. get actual date from string Today, Tomorrow?