towry
7/17/2017 - 5:52 AM

convert date string into date in swift.

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")