DateFormatter返回错误的时间

我做了一个Date的扩展,返回一个格式化的string:

extension Date { var myFormattedDate : String { let formatter = DateFormatter() formatter.timeZone = TimeZone.current formatter.dateFormat = "EEEE, MMMM d, y (HH:mm a)" return formatter.string(for: self)! } } 

在运行时,我在myFormattedDate属性中设置了一个断点。

po self印:

2017-09-05 08:50:00 +0000

po formatter.string(for: self)! 印刷:

2017年9月5日星期二(上午11:50)

可能是什么问题呢? 谢谢!

打印Date始终返回UTC时间 ,不pipe当地时区。 只要避免直接打印Date对象,如果你想在控制台中看到具有适当时区的date。