extension Formatter {
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.timeZone = TimeZone.init(identifier: "America/New_York")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
return formatter
}()
}
extension Date {
var iso8601: String {
return Formatter.iso8601.string(from: self)
}
}
extension String {
var dateFromISO8601: Date? {
return Formatter.iso8601.date(from: self) // "Mar 22, 2017, 10:22 AM"
}
}
let dateFormat:String = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let strDate: String = "2017-10-09T00:00:00.966Z"
if let dateFromString = strDate.dateFromISO8601
{
print(dateFromString.iso8601)
}
Ok, so it does not do anything with the dateFormatter.date(from: sweDate)!) then? How can I get the string value to Date?
As per my knowledge Date doesn't store the time zone so it always prints the UTC time no matter what the time zone i have used upon formatting.
So what is the solution as i have to compare my local date with the converted date. which i cannot compare with the string. Any help is appreciated.
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
check this https://mcmap.net/q/115683/-how-can-i-parse-create-a-date-time-stamp-formatted-with-fractional-seconds-utc-timezone-iso-8601-rfc-3339-in-swift. You can use the TimeZone to convert your date to string with a specific locale. – Pendragon