如何在golang中解析非传统日期?

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

How do I parse non-traditional dates in golang?

问题

我有一个日期,格式如下:

25.04.2016

我需要将其解析为 Golang 的时间对象,以便我可以将其存储在我的数据库中。

最好的方法是什么?我找不到一个标准的解析格式可以实现这个。

英文:

I have this date, in this format:

25.04.2016

I need to parse this into a Golang time object so that I can store it in my DB.

What is the best way to do so? I can't find a standard parsing format that will do so.

答案1

得分: 1

要解析Go中的日期,您需要提供一个表示日期的格式字符串,例如Mon Jan 2 15:04:05 MST 2006。在这种情况下,它看起来像这样:

t, _ := time.Parse("02.01.2006", "25.04.2016")

(playground示例:https://play.golang.org/p/6E9zshNeFG)

请查看此处的包文档:https://golang.org/pkg/time/#example_Parse

我相信您在示例格式中使用的任意日期是Go编程语言的第一天。

英文:

To parse a date in go you provide a format string that represents the date Mon Jan 2 15:04:05 MST 2006 so in this case it would look like;

t, _ := time.Parse("02.01.2006", "25.04.2016")

(play ground example; https://play.golang.org/p/6E9zshNeFG )

Check the packages docs here; https://golang.org/pkg/time/#example_Parse

I believe the arbitrary date you use as the example format is the first day of the Go programming language.

huangapple
  • 本文由 发表于 2016年4月26日 02:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/36848858.html
匿名

发表评论

匿名网友

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

确定