Last active
April 16, 2018 16:21
-
-
Save azonov/109dd7aab25bd76af65961d67677a6e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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