How to convert a date string with optional fractional seconds using Codable in Swift?

You can use two different date formatters (with and without fraction seconds) and create a custom DateDecodingStrategy. In case of failure when parsing the date returned by the API you can throw a DecodingError as suggested by @PauloMattos in comments: iOS 9, macOS 10.9, tvOS 9, watchOS 2, Xcode 9 or later The custom ISO8601 … Read more

How do I get the current Date in short format in Swift

Xcode 11 or later • Swift 5.1 or later extension TimeZone { static let gmt = TimeZone(secondsFromGMT: 0)! } extension Locale { static let ptBR = Locale(identifier: “pt_BR”) } extension Formatter { static let date = DateFormatter() } extension Date { func localizedDescription(date dateStyle: DateFormatter.Style = .medium, time timeStyle: DateFormatter.Style = .medium, in timeZone: TimeZone … Read more