How to display time ago from a date in Swift
In this short article we will see how you can display the time ago from a given date. This can be useful inside a chat or inside a social network application.
Using RelativeDateTimeFormatter (Swift 5.1)
Since Swift 5.1 Apple provide a new formatter called RelativeDateTimeFormatter. With that API you are able to display a relative date in a human readable format. This formatter also support localization.
Here a code example:
Loading...You can also change the unitsStyle to .abbreviated to have a shorter result.
Manual implementation (Swift 3/4)
Before Swift 5.1 you need to do it manually. Here is a simple extension to handle this:
Loading...This extension on Date gives you a timeAgoDisplay() method that returns a human readable string like "2 hrs ago" or "3 days ago" depending on how far back the date is.



