Golang time.Parse 定义新的格式类型

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

Golang time.Parse Defining New Format Type

问题

我正在尝试解析格式为"2017/02/28 17:07:54"的日期。我正在使用time.Parse方法。

playground示例:https://play.golang.org/p/B_hnws1AGv

但是这个方法失败了。它产生了一个时间对象:0001-01-01 00:00:00 +0000 UTC(显然不是我想要解析的日期)。

我该如何解析这种格式的日期?我的最终目标是将"2017/02/28 17:07:54"转换为"Feb 28"。请注意,我不想要"February 28",我想要"Feb 28"。

我已经查看了以下链接:

英文:

I am trying to parse a date of the format "2017/02/28 17:07:54". I am using the time.Parse method.

playground example: https://play.golang.org/p/B_hnws1AGv

This is failing. It produces a time object: 0001-01-01 00:00:00 +0000 UTC (which clearly is not the date I am trying to parse).

How can I parse a date of this format? My end goal is to convert "2017/02/28 17:07:54" to "Feb 28" Note that I do not want "February 28" I want "Feb 28."

I have looked at the following links:

答案1

得分: 1

你的格式字符串使用了错误的参考日期2006-01-02 15:04:05)。应该使用以下格式:

const longForm2 = "2006/01/02 15:04:05"
英文:

Your format string is using the wrong reference date (2006-01-02 15:04:05). It should be the following:

const longForm2 = "2006/01/02 15:04:05"

huangapple
  • 本文由 发表于 2017年3月1日 02:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/42516194.html
匿名

发表评论

匿名网友

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

确定