NSDateFormatter和stringFromDate

时间:2016-08-04 23:28:54

标签: ios swift nsdateformatter

所以,我有

let formatter: NSDateFormatter = NSDateFormatter()
formatter.dateFormat = "yyyy MM dd"

然后我有

dateLabel.text = formatter.stringFromDate(currentDate!)

但是当我这样做时:

(lldb) po dateLabel.text
▿ Optional<String>
  - Some : "2016 01 17"

(lldb) po currentDate
▿ Optional<NSDate>
  - Some : 2016-01-18 00:00:00 +0000

很明显,从2016-01-18转换为字符串结果2016 01 17.任何想法?

1 个答案:

答案 0 :(得分:0)

您需要参考UTC时区

    let dateString = "2016 01 17"
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
    let s = dateFormatter.dateFromString(dateString)
    print(s)