Created
July 17, 2017 05:52
-
-
Save towry/0c00ac663c494e045ed34c6188c9e33a to your computer and use it in GitHub Desktop.
convert date string into date in swift.
This file contains hidden or 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
| //: Playground - noun: a place where people can play | |
| import AppKit | |
| let dateFormatter = DateFormatter() | |
| dateFormatter.dateStyle = .long | |
| dateFormatter.timeStyle = .none | |
| let dateStr = "2017-10-12" | |
| // Use abbreviation is discouraged, because it is not standardized. | |
| // If we do not set the timezone, the output will be incorrect. | |
| dateFormatter.timeZone = TimeZone(abbreviation: "UTC") | |
| dateFormatter.dateFormat = "yyyy-MM-dd" | |
| dateFormatter.locale = Locale(identifier: "zh_Hans_CN") | |
| let date = dateFormatter.date(from: dateStr) | |
| print(date ?? "none") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment