Skip to content

Instantly share code, notes, and snippets.

@eleev
Last active March 24, 2017 11:19
Show Gist options
  • Save eleev/1ba837e50687a96b5e941d5ad6fd0518 to your computer and use it in GitHub Desktop.
Save eleev/1ba837e50687a96b5e941d5ad6fd0518 to your computer and use it in GitHub Desktop.
Extension for Date class that provides easy-to-use feature for convering date to string. Swift 3
extension Date {
struct Formatter {
static let shortDate = DateFormatter(dateStyle: .short)
static let longDate = DateFormatter(dateStyle: .long)
static let fullDate = DateFormatter(dateStyle: .full)
}
var shortDate: String { return Formatter.shortDate.string(from: self) }
var longDate: String { return Formatter.longDate.string(from: self) }
var fullDate: String { return Formatter.fullDate.string(from: self) }
var mediumTime: String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.locale = Locale.current
return dateFormatter.string(from: self)
}
var longTime: String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
dateFormatter.locale = Locale.current
return dateFormatter.string(from: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment