GO:在GO语言中进行日期和时间解析

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

GO: Date and Time Parsing in GO

问题

我有一个格式化字符串"day/month/year, hour:minute:second",我该如何解析成时间对象?

我尝试了以下代码:

const longForm = "5/01/2015, 12:00:00"
t1, _ := time.Parse(longForm, "5/01/2015, 12:00:00")

但是我得到了一些UTC时间,但这对于比较时间并不有用,因为它们都得到了相同的UTC时间。有什么帮助吗?

英文:

I have format string "day/month/year, hour:minute:second"

How do I parse into a time object?

I tried:

const longForm = "5/01/2015, 12:00:00"

t1, _ := time.Parse(longForm, "5/01/2015, 12:00:00") 

0001-01-01 00:00:00 +0000 UTC

I get some UTC time, but this is not helpful if I want to compare times because I get the same UTC time for them all. Any help?

答案1

得分: 3

规则 #1 的战斗俱乐部,呃,检查你的错误。

话虽如此,时间解析的格式在文档中有定义(向下滚动到常量部分)。

对于你的具体问题,格式是 1/_2/2006, 15:04:05

<kbd>playground</kbd>

英文:

Rule #1 of fightclub, err Go, check your errors.

That being said, the format for time parsing is defined in the documentation (scroll down to constants).

For your specific question, the format is 1/_2/2006, 15:04:05.

<kbd>playground</kbd>

huangapple
  • 本文由 发表于 2015年5月2日 09:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/29997234.html
匿名

发表评论

匿名网友

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

确定