ios - Time converter "071953Z" (ZULU TO CURRENT TIME) -
i'm using api gives me following time format:
"071953z"
07 day
19 hours
53 minutes
z zulu time
as can see, there no current month , year, want added dont know how.
could me convert current time? thanks!
i think best bet store partial-datetime in datecomponents. can add month , year , use foundation's calendar api want:
extension datecomponents { static func from(str: string) -> datecomponents? { guard str.characters.count == 7 else { return nil } var components = datecomponents() components.timezone = timezone(secondsfromgmt: 0) components.calendar = calendar(identifier: .gregorian) let ranges = [0,2,4].map { return str.index(str.startindex, offsetby: $0)..<str.index(str.startindex, offsetby: $0 + 2) } if let day = int(str[ranges[0]]) { components.day = day } else { return nil } if let hour = int(str[ranges[1]]), hour < 24 { components.hour = hour } else { return nil } if let minute = int(str[ranges[2]]), minute < 60 { components.minute = minute } else { return nil } return components } } // formatter convert date local time. configure taste let formatter = dateformatter() formatter.datestyle = .medium formatter.timestyle = .medium if var components = datecomponents.from(str: "071953z") { components.year = 2017 components.month = 4 if let date = components.date { print(formatter.string(from: date)) } else { print("\(components) not make valid date") } }
Comments
Post a Comment