【问题标题】:Dateformatter Result日期格式化程序结果
【发布时间】:2021-05-20 13:50:19
【问题描述】:

这是我的 DateFormatter 代码

 let formatter = DateFormatter()
 formatter.dateFormat = "MMM dd"
 let dateString = formatter.string(from: date)

现在:

po dateString 

给出结果“5 月 18 日”

po date gives the result 2021-05-17 18:30:00 +0000

这对我来说没有意义 - 为什么将 Date May 17 2021 转换为字符串 May 18?

【问题讨论】:

  • +0000 和 18h30?您是否在印度,时区差异目前为 5:30h?那很正常,不是吗? UTC 时间可能是 5 月 17 日 18:30,但在印度时间,这意味着 5 月 18 日 00:00。

标签: ios swift date dateformatter


【解决方案1】:

Date 对象记录了地球上任何地方的一个瞬间。

根据您所在的时区,该瞬间将在不同的日历日(日/月/年)。

默认情况下,日期格式化程序在设备的当前时区工作。

当您使用 po dateprint(date) 记录日期时,日期的默认描述会使用 ISO 8601 日期格式以 UTC 格式显示该日期。根据用户所在的时区,UTC 中的日期可能与本地时区中的日期不同

如果要在用户的本地时区记录日期,请使用 po date.description(with: Locale.current)po DateFormatter.localizedString(from: date, dateStyle: .medium, timeStyle: .medium)

【讨论】:

    【解决方案2】:

    这取决于使用的格式化程序的当前时区,基于 0 的使用

    formatter.timeZone = TimeZone(abbreviation: "UTC")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2015-06-17
      • 2017-04-29
      • 1970-01-01
      相关资源
      最近更新 更多