Skip to content

Instantly share code, notes, and snippets.

@vialyx
Last active March 21, 2018 18:59
Show Gist options
  • Save vialyx/53c18e4c368db37785c784e7a330c8b0 to your computer and use it in GitHub Desktop.
Save vialyx/53c18e4c368db37785c784e7a330c8b0 to your computer and use it in GitHub Desktop.
let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.unitsStyle = .full
dateComponentsFormatter.includesApproximationPhrase = true
dateComponentsFormatter.includesTimeRemainingPhrase = true
dateComponentsFormatter.allowedUnits = [.minute, .hour]
let timeIntervalInPast: TimeInterval = -3666
let nowDate = Date()
let pastDate = nowDate.addingTimeInterval(timeIntervalInPast)
guard let formattedString = dateComponentsFormatter.string(from: pastDate, to: nowDate) else { fatalError() }
print("Remaining time: \(formattedString)")
// Remaining time: About 1 hour, 1 minute remaining
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment