Skip to content

Instantly share code, notes, and snippets.

@towry
Created July 17, 2017 05:52
Show Gist options
  • Select an option

  • Save towry/0c00ac663c494e045ed34c6188c9e33a to your computer and use it in GitHub Desktop.

Select an option

Save towry/0c00ac663c494e045ed34c6188c9e33a to your computer and use it in GitHub Desktop.
convert date string into date in swift.
//: 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