本文介绍了在Go中解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析由tar生成的时间戳,如2011-01-19 22:15,但无法解决time.Parse的时髦API。
I'm trying to parse a timestamp as produced by tar such as '2011-01-19 22:15' but can't work out the funky API of time.Parse.
以下生成解析时间2011-01-19 22:15:月超出范围
The following produces 'parsing time "2011-01-19 22:15": month out of range'
package main
import (
"fmt"
"time"
)
func main () {
var time , error = time.Parse("2011-01-19 22:15","2011-01-19 22:15")
if error != nil {
fmt.Println(error.String())
return
}
fmt.Println(time)
}
推荐答案
按照时间包文档
h是Unix时间 1136243045
。 (认为
它为 01/02 03:04:05 PM '06 -0700
。
要定义您自己的格式,写下
标准时间看起来像
格式化您的方式。
which is Unix time 1136243045
. (Think of it as 01/02 03:04:05PM '06 -0700
.) To define your own format, write down what the standard time would look like formatted your way.
例如,
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse("2006-01-02 15:04", "2011-01-19 22:15")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(time.SecondsToUTC(t.Seconds()))
}
Output: Wed Jan 19 22:15:00 UTC 2011
这篇关于在Go中解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!