Swift:夏令时日期解析

发布于 2025-01-12 21:20:45 字数 990 浏览 0 评论 0原文

我已从服务器收到字符串形式的日期时间。如果日期时间是夏令时发生的确切时间,我无法解析日期。 时如何处理

当日期时间字符串为 "2022-03-13T02:00:00.000000" 输出:

2022-03-13 05:00:00 +0000
2022-03-13 06:59:00 +0000
failed
2022-03-13 07:00:00 +0000
2022-03-13 08:00:00 +0000

代码:

extension String {
    
    func date(using format: String) {
        let df = DateFormatter()
        df.dateFormat = format
        df.timeZone = .current
        if let date = df.date(from: self) {
            print(date)
        }
        else {
            print("failed")
        }
    }
}

"2022-03-13T00:00:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T01:59:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T02:00:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T03:00:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T04:00:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")

I have received date-time as String from server. I am unable to parse Date if date time is exact time of daylight saving time happen. How to handle when date-time string is "2022-03-13T02:00:00.000000"

Output:

2022-03-13 05:00:00 +0000
2022-03-13 06:59:00 +0000
failed
2022-03-13 07:00:00 +0000
2022-03-13 08:00:00 +0000

Code:

extension String {
    
    func date(using format: String) {
        let df = DateFormatter()
        df.dateFormat = format
        df.timeZone = .current
        if let date = df.date(from: self) {
            print(date)
        }
        else {
            print("failed")
        }
    }
}

"2022-03-13T00:00:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T01:59:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T02:00:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T03:00:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
"2022-03-13T04:00:00.000000".date(using: "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花ソ最美的依靠 2025-01-19 21:20:45

您只需要设置日期格式化程序的日历属性。这也将避免使用用户设备的日历,这可能会导致解析错误的年份。在解析固定日期格式时,不要忘记始终将日期格式化程序的区域设置设置为“en_US_POSIX”:

extension String {
    func date(using format: String) -> Date? {
        let df = DateFormatter()
        df.calendar = .init(identifier: .iso8601)
        df.locale = .init(identifier: "en_US_POSIX")
        df.timeZone = .current
        df.dateFormat = format
        df.date(from: self) 
    }
}

另请注意,每次调用此方法时,此方法都会创建一个新的日期格式化程序。您应该避免这种情况以及创建静态格式化程序。我还要确保日期字符串是否使用当前时区。通常是 UTC 时区。

You just need to set your date formatter's calendar property. This will also avoid using the user device's calendar which might result in parsing the wrong year. Don't forget to always set your date formatter's locale to "en_US_POSIX" when parsing a fixed date format:

extension String {
    func date(using format: String) -> Date? {
        let df = DateFormatter()
        df.calendar = .init(identifier: .iso8601)
        df.locale = .init(identifier: "en_US_POSIX")
        df.timeZone = .current
        df.dateFormat = format
        df.date(from: self) 
    }
}

Note also that this approach will create a new date formatter every time you call this method. You should avoid that as well creating a static formatter. I would also make sure if the date string is using the current timezone. Usually it is UTC timezone.

深者入戏 2025-01-19 21:20:45

采用 ISO8601DateFormatter 要将这些字符串转换为日期,然后当您需要将它们显示到应用程序中时,请使用设置为所需时区的日期格式化程序。

这样,您就不会在夏令时转换方面遇到任何问题。

当然,当您需要将日期重新编码为String以进行传输时,请再次使用ISO8601DateFormatter以保持用于接收它们的相同格式。

Adopt the ISO8601DateFormatter To convert those strings into dates, then when you need to display them into your app use a date formatter set to the desired time zone.

In this way you won’t have any trouble with daylight savings conversions.

Of course when you need to re-encode a date into a String For transmitting it use again the ISO8601DateFormatter In order to keep the same format used for receiving them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文