Skip to content

Instantly share code, notes, and snippets.

@marcos1262
Created October 27, 2018 14:11
Show Gist options
  • Save marcos1262/34acf7f314af727eebb4c73f8bc64aff to your computer and use it in GitHub Desktop.
Save marcos1262/34acf7f314af727eebb4c73f8bc64aff to your computer and use it in GitHub Desktop.
Abbreviated date interval String formatting
var timeString: String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.month, .day, .hour, .minute]
formatter.unitsStyle = .abbreviated
formatter.zeroFormattingBehavior = .dropAll
if let date = self.date, let interval = formatter.string(from: date, to: Date()) {
return "\(interval) ago"
}
return "unknown"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment