简体   繁体   English

NSDateFormatter和stringFromDate

[英]NSDateFormatter and stringFromDate

So, I have 所以我有

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

And then I have 然后我有

dateLabel.text = formatter.stringFromDate(currentDate!)

But when I do this: 但是当我这样做时:

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

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

Obivously, conversion from 2016-01-18 to string results in 2016 01 17. Any ideas? 显而易见,从2016-01-18转换为字符串结果2016 01 17.任何想法?

You need to refer to UTC timezone 您需要参考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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM