Skip to content

Instantly share code, notes, and snippets.

@azonov
Last active April 16, 2018 16:21
Show Gist options
  • Save azonov/109dd7aab25bd76af65961d67677a6e1 to your computer and use it in GitHub Desktop.
Save azonov/109dd7aab25bd76af65961d67677a6e1 to your computer and use it in GitHub Desktop.
public struct Formatters {
public enum DateFormatterType {
case ISO8601
case medium
}
static func string(from date: Date, for type: DateFormatterType) -> String {
return formatter(for: type).string(from: date)
}
static func date(from string: String, for type: DateFormatterType) -> Date? {
return formatter(for: type).date(from: string)
}
private static let iso8601: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
return dateFormatter
}()
private static let medium: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
return dateFormatter
}()
private static func formatter(for type: DateFormatterType) -> DateFormatter {
switch type {
case .ISO8601:
return iso8601
case .medium:
return medium
}
}
}
extension DateFormatter {
static var fmt = Formatters.self
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment