I have created an extension to manage my dates parsing but I have an issue with date of 2099/12/31!
Here's my extension which gives me date object from YYYYMMdd string and reverse fonction.
extension Date {
// build date from string with YYYYMMDD format
static func dateCSDFrom(_ stringDate : String) -> Date?{
let dateFormatter = CSDDateFormatter()
return dateFormatter.date(from: stringDate)
}
// reverse function
func stringCSDFromDate() -> String{
return Date.CSDDateFormatter().string(from: self)
}
private static func CSDDateFormatter() -> DateFormatter{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYYMMdd"
return dateFormatter
}}
let dateString = "20991231"
let date = Date.dateCSDFrom(dateString)
print("date object: \(date)")
print("date string: \(date?.stringCSDFromDate())")
Use yyyy instead of YYYY Take a look at this : http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns