如何将日期字符串转换为时间字符串?

发布于 2025-02-12 07:04:29 字数 563 浏览 0 评论 0原文

我有一个看起来像这样的字符串:

"date": "2022-06-30T02:15:00.000+07:00"

我格式化了它以将其转换为“ hh:mm”这样:

func formatTime(string: String) -> String {
    let dateFormatterGet = DateFormatter()
    dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"

    let dateFormatterPrint = DateFormatter()
    dateFormatterPrint.dateFormat = "HH:mm"

    let date: Date? = dateFormatterGet.date(from: string)
    return dateFormatterPrint.string(from: date ?? Date())
}

我希望它返回的结果为09:15,但它返回02:15。 有人可以告诉我我在哪里做错了吗?谢谢

I have a string that looks like this:

"date": "2022-06-30T02:15:00.000+07:00"

And I formatted it to convert to "HH:mm" like this:

func formatTime(string: String) -> String {
    let dateFormatterGet = DateFormatter()
    dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"

    let dateFormatterPrint = DateFormatter()
    dateFormatterPrint.dateFormat = "HH:mm"

    let date: Date? = dateFormatterGet.date(from: string)
    return dateFormatterPrint.string(from: date ?? Date())
}

The result I want it returns is 09:15, but it returns 02:15.
Can someone tell me where I went wrong? Thank you

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

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

发布评论

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

评论(2

一世旳自豪 2025-02-19 07:04:29

“ 2022-06-30T02:15:00.000+07:00”是时区的日期,距离UTC时区7小时。在UTC中的日期是“ 2022-06-29T19:15:00z”,

而DateFormatterPrint根据电话设置

需要设置UTC TimeZone的默认场所配置

"2022-06-30T02:15:00.000+07:00" is the date in the time zone plus 7 hours from the UTC time zone. Than this date in UTC is “ 2022-06-29T19:15:00Z”

while dateFormatterPrint has a default locale configuration according to the phone settings

You need to set UTC timezone

池木 2025-02-19 07:04:29

在这种情况下,您可能需要先将日期时间转换为当地时间:

转换为Localtime:

func utcToLocal(dateStr: String) -> String? {
  let dateFormatter = DateFormatter()
  dateFormatter.dateFormat = "HH:mm"
  dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
  if let date = dateFormatter.date(from: dateStr) {
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.dateFormat = "HH:mm"

    return dateFormatter.string(from: date)
  }
  return nil
}

,然后:

let utcTime = formatTime(string: "2022-06-30T02:15:00.000+07:00")
let localTime = utcToLocal(dateStr: utcTime)
print(localTime)

结果:

09:15

希望这有助于您解决问题。

In this case you might want to convert the DateTime to your local time first:

Convert To LocalTime:

func utcToLocal(dateStr: String) -> String? {
  let dateFormatter = DateFormatter()
  dateFormatter.dateFormat = "HH:mm"
  dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
  if let date = dateFormatter.date(from: dateStr) {
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.dateFormat = "HH:mm"

    return dateFormatter.string(from: date)
  }
  return nil
}

And then:

let utcTime = formatTime(string: "2022-06-30T02:15:00.000+07:00")
let localTime = utcToLocal(dateStr: utcTime)
print(localTime)

The Result:

09:15

I hope this help you solve your problem.

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