Created
April 25, 2018 14:42
-
-
Save jbarros35/8f280d6300f6e97933e0400d16e9a530 to your computer and use it in GitHub Desktop.
Convert JavaScript dates and Swift
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
// REMARK: format date for better display | |
func convertDate(date: Date) -> String? { | |
let dateFormatterGet = DateFormatter() | |
dateFormatterGet.dateFormat = "yyyyMMdd HH:mm:ss" | |
let dateString = dateFormatterGet.string(from: date) | |
return dateString | |
} | |
// REMARK: server sends dates on JS string format | |
func convertJSDate(date: String) -> String? { | |
let dateFormatterGet = DateFormatter() | |
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" | |
if let date = dateFormatterGet.date(from: date) { | |
return convertDate(date: date) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment