【问题标题】:Golang parsing date in RFC822Z format without leading zero [duplicate]Golang 以 RFC822Z 格式解析日期,不带前导零 [重复]
【发布时间】:2022-12-10 22:09:17
【问题描述】:

我有一个日期字符串,我无法控制我正在尝试将其解析为日期。

该格式最类似于 RFC822Z。

RFC822Z =“06 年 1 月 2 日 15:04 -0700”

参考:https://yourbasic.org/golang/format-parse-string-time-date-example/

但是,它没有前导零。

示例:“2022 年 12 月 5 日 20:15:21 +0000”

我在其他帖子中看到的方式是编写手册格式。

parseTime, timeParseError = time.Parse("2 Jan 2006 15:04:21 -0700", stringDate)

但是,当我尝试这样做时,我收到警告:

parsing time "2 Jan 2006 15:04:21 -0700" as "2 Jan 2006 15:04:21 -0700": cannot parse " -0700" as "1" (SA1002)

不出所料,尽管有警告,但仍然运行它。

【问题讨论】:

    标签: date go date-parsing


    【解决方案1】:

    您的时间格式不匹配 - 在您的示例中,您使用的是“2022 年 12 月 5 日”,但您使用的是“2006 年 1 月 2 日”,在您的参考格式中,您使用的是“15:04:21”,但它应该是“15: 04:05”。

    你的参考格式应该是2 Jan 2006 15:04:05 -0700而不是2 Jan 06 15:04:21 -0700

    https://go.dev/play/p/Shc381WgB6_7

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2015-09-18
      相关资源
      最近更新 更多