Swift DateFormatter从带有(EEST)的字符串中解析?

huangapple go评论73阅读模式
英文:

Swift DateFormatter from String with (EEST)?

问题

我正在尝试解析来自API的字符串日期为Date()。

日期字符串是:31-07-2023 12:44 (EEST)

我的代码:

let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy hh:mm (zzzz)"

if let ptdate = formatter.date(from: prayer.getString("time")) {
    // 无法工作!
}

我相信我正在使用的日期格式dd-MM-yyyy hh:mm zzzz中缺少了一些内容,但我不确定是什么,因为我已经花了数小时搜索日期格式。

英文:

i am trying to parse string date which is coming from an API to Date().

the date string is: 31-07-2023 12:44 (EEST)

My code:

let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy hh:mm (zzzz)" // "yyyy-MM-dd h:mm a"

if let ptdate = formatter.date(from: prayer.getString("time")) {
    // Not working!
}

i believe there is something missing in date format i am using which is dd-MM-yyyy hh:mm zzzz, i am not sure what it is, since i've spent hours googling date formats.

答案1

得分: 1

let dateString = "31-07-2023 12:44 (EEST)"

let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy' 'HH:mm' ('z')"

if let ptdate: Date = formatter.date(from: dateString) {
    print(ptdate)
}
英文:

For the dateFormat string you need to:

  • Escape all string content with '
  • Use 24h time format
  • Use time zone specific non-location field code "z" (equivalents are: zz, zzz, zzzz)
let dateString = "31-07-2023 12:44 (EEST)"

let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy' 'HH:mm' ('z')"

if let ptdate: Date = formatter.date(from: dateString) {
    print(ptdate)
}

答案2

得分: 1

我是你的中文翻译,以下是翻译好的部分:

我是这样做的(不需要日期格式化器):

    let dateString = "31-07-2023 12:44 (EEST)"
    let format: Date.FormatString = "\(day: .twoDigits)-\(month: .twoDigits)-\(year: .defaultDigits) \(hour: .twoDigits(clock: .twentyFourHour, hourCycle: .oneBased)):\(minute: .twoDigits) (\(timeZone: .specificName(.short)))"
    var strategy = Date.ParseStrategy.fixed(format: format, timeZone: .current)
    let date = try? Date(dateString, strategy: strategy)

请注意,我已经将 HTML 编码字符 " 转换为正常的双引号。如果你有任何其他需要或问题,请随时提出。

英文:

I did it like this (no date formatter needed):

let dateString = "31-07-2023 12:44 (EEST)"
let format: Date.FormatString = "\(day: .twoDigits)-\(month: .twoDigits)-\(year: .defaultDigits) \(hour: .twoDigits(clock: .twentyFourHour, hourCycle: .oneBased)):\(minute: .twoDigits) (\(timeZone: .specificName(.short)))"
var strategy = Date.ParseStrategy.fixed(format: format, timeZone: .current)
let date = try? Date(dateString, strategy: strategy)

答案3

得分: 1

"EEST"是一个标准的时区缩写,因此您应该使用 zzz,并且我也假设时间格式是24小时制,所以配置日期格式化程序如下:

formatter.dateFormat = "dd-MM-yyyy HH:mm (zzz)"

更新:

似乎单个 z 也可以工作:"dd-MM-yyyy HH:mm (z)"

英文:

"EEST" is a standard time zone abbreviation so you should use zzz and I also assume that the time format is 24h so configure the date formatter like this

formatter.dateFormat = "dd-MM-yyyy HH:mm (zzz)"

Update

Seems like a single z also works "dd-MM-yyyy HH:mm (z)"

huangapple
  • 本文由 发表于 2023年7月13日 22:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定