ios - DateFormatter 为 iOS 11(转换为 UTC)和 iOS 10(未转换为 UTC)提供不同的日期

标签 ios swift xcode

将时间字符串转换为日期对象的方法:

class func StringToDate(format: String, date: String) -> Date{
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.date(from: date)!
    }

格式:hh:mm a

时间字符串:10:00 AM

结果:

字符串时间:10:00 AM,对象时间:2000-01-01 10:00:00 +0000 //ios 10

字符串时间:10:00 AM,对象时间:2000-01-01 04:30:00 +0000 //ios 11

对于 iOS v11 结果很好,但对于 iOS v10 它是错误的,我想要 UTC 格式的结果。

我试过:

1. formatter.timeZone = NSTimeZone.local
2. formatter.timeZone = TimeZone(identifier: "UTC")
3. formatter.timeZone = TimeZone.ReferenceType.system

最佳答案

请添加这个(这可能是因为您的系统正在选择当前时区)-

formatter.timeZone = TimeZone.init(abbreviation: “UTC”)

关于ios - DateFormatter 为 iOS 11(转换为 UTC)和 iOS 10(未转换为 UTC)提供不同的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51646888/

相关文章:

ios - 使用未声明的标识符,仅在分析应用程序时

ios - Dealloc 正在推迟

ios - View Controller 关闭并重新出现以前的 View Controller

objective-c - 从 Objective-C 中的实例十六进制获取类

ios - swift ,相机图像不返回

ios - UISearchController 没有响应

xcode - 在 iOS 上升级到 PhoneGap 2.0 后,JSONKit.m 中出现多个 "Format String Issue"警告

ios - MKPinAnnotationView 不设置自定义图像

ios - 用两个 CALayer 叠加绘制

ios - UIScrollViewDelegate 中的委托(delegate)方法不响应 TableView 滚动事件