Last active
February 21, 2018 10:18
-
-
Save cjae/e670495a9cfd4b46a37d96578eb80102 to your computer and use it in GitHub Desktop.
Sample code to properly format Sql timestamp in JSON from a web service.
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
// Language: Swift 4 | |
func getDayFromSqlTime(timeStamp: String) -> String { | |
let dateFormatter = DateFormatter() | |
/* | |
This format is based on what is returned from the webservice. My webservice returns "2018-12-12 20:19:22". | |
Every character has to be represented for this to work properly else Xcode would throw an error. | |
For example if your webservice returns "2018-02-21T10:10:14", then valid format would be "yyyy-MM-dd'T'HH:mm:ss", | |
if "2018-02-02 06:50:16 +0000", then format is "yyyy-MM-dd HH:mm:ss +zzzz". | |
*/ | |
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" | |
let displayDate1 = dateFormatter.date(from: timeStamp) | |
/* | |
This is the readable format I want displayed on my app. For example, this would return "03 Dec, 2018". | |
*/ | |
dateFormatter.dateFormat = "dd MMM, yyyy" | |
let displayDate2 = dateFormatter.string(from: displayDate1!) | |
return displayDate2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment