SALE ENDS TODAY: Save 50% on all Swift books and bundles! >>

How to show a relative date and time using RelativeDateTimeFormatter

Swift version: 5.1

Paul Hudson    @twostraws   

If you want to format dates and times in the form “5 hours ago” or “3 months ago”, Apple gives us a dedicated formatter called RelativeDateTimeFormatter. This is localized for many languages, so you’ll automatically get back strings that work in French, German, Chinese, and more, all depending on the user’s locale.

Here’s an example to get you started:

// the date you want to format
let exampleDate = Date().addingTimeInterval(-15000)

// ask for the full relative date
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full

// get exampleDate relative to the current date
let relativeDate = formatter.localizedString(for: exampleDate, relativeTo: Date())

// print it out
print("Relative date is: \(relativeDate)")

That will print “Relative date is: 4 hours ago”.

“Full” has a precise meaning here: we’ll get back things like “2 months ago”, and if you prefer you can try spell out mode to get “two months ago” or even short mode to get “2 mo. ago”.

Having that second relativeTo parameter available allows us to calculate relative values between two arbitrary dates, rather than one date and the current date:

let relativeDate2 = formatter.localizedString(for: someDate, relativeTo: someOtherDate)

Tip: Although relative time formatters are great for things in recent history – the last few months, perhaps – they are less useful for larger time gaps. So, you might want to try checking whether your date is over six months ago, and if so use a custom formatter instead to give the specific date.

SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.

Save 50% on all books and bundles

The biggest ever Hacking with Swift sale is now on, letting you save 50% on all books and bundles. Learn something new with Swift and enjoy great savings while the sale lasts!

Click here to save 50% in our Black Friday sale!

Available from iOS 13.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.7/5