localizedString(for:relativeTo:)
Formats the date interval from the reference date to the specified date using the formatter’s calendar.
Declaration
func localizedString(for date: Date, relativeTo referenceDate: Date) -> StringParameters
- date:
The end date of the interval to format.
- referenceDate:
The start date of the interval to format.
Return Value
A string that represents the date interval between two dates.
Discussion
let tenMinutesAgo = Date(timeIntervalSinceNow: -600)
let twoMintuesAhead = Date(timeIntervalSinceNow: 120)
let formatter = RelativeDateTimeFormatter()
print(formatter.localizedString(for: tenMinutesAgo, relativeTo: twoMintuesAhead))
// Outputs: 12 minutes ago