Swift:夏令时日期解析
我已从服务器收到字符串形式的日期时间。如果日期时间是夏令时发生的确切时间,我无法解析日期。 时如何处理
当日期时间字符串为 "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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要设置日期格式化程序的日历属性。这也将避免使用用户设备的日历,这可能会导致解析错误的年份。在解析固定日期格式时,不要忘记始终将日期格式化程序的区域设置设置为“en_US_POSIX”:
另请注意,每次调用此方法时,此方法都会创建一个新的日期格式化程序。您应该避免这种情况以及创建静态格式化程序。我还要确保日期字符串是否使用当前时区。通常是 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:
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.
采用
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 theISO8601DateFormatter
In order to keep the same format used for receiving them.