Skip to content

Instantly share code, notes, and snippets.

@jbarros35
Created April 25, 2018 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarros35/c72a50b7dca030e72d93f13989b556d2 to your computer and use it in GitHub Desktop.
Save jbarros35/c72a50b7dca030e72d93f13989b556d2 to your computer and use it in GitHub Desktop.
Convert JavaScript dates and Swift
// 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