英文:
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"。
我已经查看了以下链接:
- https://stackoverflow.com/questions/36807596/how-to-convert-date-format-in-golang
- https://stackoverflow.com/questions/25845172/parsing-date-string-in-golang
英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论