DateFormatter返回错误的时间 [英] DateFormatter returns wrong time

查看:94
本文介绍了DateFormatter返回错误的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Date做了一个返回格式化字符串的扩展名:

I did an extension for Date that returns a formatted 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)!
    }
}

在运行时,我在<$内设置断点c $ c> myFormattedDate property。

On runtime, I set a breakpoint inside the myFormattedDate property.

po self 打印:


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

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

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


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

可能是什么问题?
谢谢!

What could be the problem? Thanks!

推荐答案

打印日期始终返回 UTC时间。如果您想查看日期,请直接避免直接打印日期对象在控制台中使用适当的时区。

Printing a Date always returns an UTC time, regardless of the local time zone. Just avoid printing a Date object directly if you want to see the date with the proper time zone in your console.

这篇关于DateFormatter返回错误的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆