Golang的time.Parse()函数返回的是0001-01-01 00:00:00 +0000 UTC。

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

Golang time.Parse() get 0001-01-01 00:00:00 +0000 UTC

问题

这是我的代码:

t, _ := time.Parse("12/1/2015 12:00:00", "12/8/2015 12:00:00")
fmt.Println(t)

这是结果:0001-01-01 00:00:00 +0000 UTC

如何获取正确的日期字符串?

谢谢!

英文:

This is my code:

t, _ := time.Parse("12/1/2015 12:00:00", "12/8/2015 12:00:00")
fmt.Println(t)

This is the result: 0001-01-01 00:00:00 +0000 UTC

How to get correct the date string?

Thanks!

答案1

得分: 11

你需要使用参考时间戳2006-01-02 15:04:05作为布局参数(playground):

t, _ := time.Parse("1/2/2006 15:04:05", "12/8/2015 12:00:00")
英文:

You need to use the reference timestamp 2006-01-02 15:04:05 for the layout parameter (playground):

t, _ := time.Parse("1/2/2006 15:04:05", "12/8/2015 12:00:00")

huangapple
  • 本文由 发表于 2015年12月8日 19:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/34154779.html
匿名

发表评论

匿名网友

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

确定