[英]Swift - Retrieve timezone from ISO8601 date string
I have dates saved in a database in this format - "yyyy-MM-dd'T'HH:mm:ssZ". 我以这种格式将日期保存在数据库中 - “yyyy-MM-dd'T'HH:mm:ssZ”。 For an example, "2018-05-17T11:15:00+0330".
例如,“2018-05-17T11:15:00 + 0330”。 The time zone varies, it is in whatever user's local time zone is.
时区不同,它在任何用户的本地时区。
I want to retrieve and display the date like "May 17, 2018 11.15AM", keeping the date/time in the same time zone as it was saved. 我想检索并显示日期,如“2018年11月17日上午11点15分”,将日期/时间保存在与保存时相同的时区。 How can I do that?
我怎样才能做到这一点?
I tried this. 我试过这个。
let dateString = "2018-05-17T11:15:00+0330"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
//formatter.timeZone = ???
print(formatter.date(from: dateString))
But I get the Date object in local timezone and converting that to date string displays the date in my local timezone as well. 但我在本地时区获取Date对象并将其转换为日期字符串也会在我的本地时区显示日期。
Is there anything like this, https://stackoverflow.com/a/46216251/1373592 , in Swift? 在Swift中有这样的东西, https ://stackoverflow.com/a/46216251/1373592吗?
Thanks. 谢谢。
What you are really trying to do, is to ignore the time zone. 你真正想做的是忽略时区。
You can just strip off the last 5 characters from the date string, parse it in one format, and format it in another format: 您可以从日期字符串中删除最后5个字符,以一种格式解析它,并以另一种格式对其进行格式化:
let dateString = "2018-05-17T11:15:00+0330"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let date = formatter.date(from: String(dateString.dropLast(5)))! // note the stripping off here
formatter.dateFormat = "MMMM d, yyyy hh.mma"
print(formatter.string(from: date))
Note that you can't compare this date with other dates because you've stripped its time zone off. 请注意,您无法将此日期与其他日期进行比较,因为您已将其时区删除。
Also, SwiftDate is a similar library to JodaTime. 此外, SwiftDate是一个类似于JodaTime的库。
Here's a little extension to TimeZone
I just whipped up. 这是我刚刚掀起的
TimeZone
的一个小扩展。 You can use it to create a TimeZone
from an ISO8601 date string. 您可以使用它从ISO8601日期字符串创建
TimeZone
。 It will handle timezones in the string in the following formats: 它将以下列格式处理字符串中的时区:
Here is the extension: 这是扩展名:
extension TimeZone {
init?(iso8601: String) {
let tz = iso8601.dropFirst(19) // remove yyyy-MM-ddTHH:mm:ss part
if tz == "Z" {
self.init(secondsFromGMT: 0)
} else if tz.count == 3 { // assume +/-HH
if let hour = Int(tz) {
self.init(secondsFromGMT: hour * 3600)
return
}
} else if tz.count == 5 { // assume +/-HHMM
if let hour = Int(tz.dropLast(2)), let min = Int(tz.dropFirst(3)) {
self.init(secondsFromGMT: (hour * 60 + min) * 60)
return
}
} else if tz.count == 6 { // assime +/-HH:MM
let parts = tz.components(separatedBy: ":")
if parts.count == 2 {
if let hour = Int(parts[0]), let min = Int(parts[1]) {
self.init(secondsFromGMT: (hour * 60 + min) * 60)
return
}
}
}
return nil
}
}
And a test: 并测试:
print(TimeZone(iso8601: "2018-05-17T11:15:00+0330"))
You can combine this parsing and formatting so the final result is in the original timezone. 您可以将此解析和格式组合在一起,以便最终结果位于原始时区中。
let dateString = "2018-05-17T11:15:00+0330"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
formatter.locale = Locale(identifier: "en_US_POSIX")
if let date = formatter.date(from: dateString), let tz = TimeZone(iso8601: dateString) {
let newFmt = DateFormatter()
newFmt.dateStyle = .medium
newFmt.timeStyle = .short
newFmt.timeZone = tz
let newString = newFmt.string(from: date)
print(newString)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.