如何解析“2019-09-19 04:03:01.770080087 +0000 UTC”时间戳
How to parse "2019-09-19 04:03:01.770080087 +0000 UTC" timestamp
我将如何解析这个时间戳?
"2019-09-19 04:03:01.770080087 +0000 UTC"
我尝试了以下方法:
formatExample := obj.CreatedOn // obj.CreatedOn = "2019-09-19 04:03:01.770080087 +0000 UTC"
time, err := time.Parse(formatExample, obj.CreatedOn)
check(err)
fmt.Println(time)
但我得到的输出是:
0001-01-01 00:00:00 +0000 UTC
Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be
Mon Jan 2 15:04:05 -0700 MST 2006
would be interpreted if it were the value; it serves as an example of the input format. The same interpretation will then be made to the input string.
formatExample := "2006-01-02 15:04:05.999999999 -0700 MST"
您传递给解析的时间格式不是 "example" 格式。每个时间字段都有一个不同的值:
Mon Jan 2 15:04:05 -0700 MST 2006
例如,如果您想用您的格式描述年份,您必须使用 2006。因此您的格式必须是:
2006-01-02 15:04:05.000000000 -0700 MST
轻轻一点就可以了
package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02 15:04:05 -0700 MST"
t, _ := time.Parse(layout, "2019-09-19 04:03:01.770080087 +0000 UTC")
fmt.Println(t)
}
输出:
2019-09-19 04:03:01.770080087 +0000 UTC
我将如何解析这个时间戳?
"2019-09-19 04:03:01.770080087 +0000 UTC"
我尝试了以下方法:
formatExample := obj.CreatedOn // obj.CreatedOn = "2019-09-19 04:03:01.770080087 +0000 UTC"
time, err := time.Parse(formatExample, obj.CreatedOn)
check(err)
fmt.Println(time)
但我得到的输出是:
0001-01-01 00:00:00 +0000 UTC
Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be
Mon Jan 2 15:04:05 -0700 MST 2006
would be interpreted if it were the value; it serves as an example of the input format. The same interpretation will then be made to the input string.
formatExample := "2006-01-02 15:04:05.999999999 -0700 MST"
您传递给解析的时间格式不是 "example" 格式。每个时间字段都有一个不同的值:
Mon Jan 2 15:04:05 -0700 MST 2006
例如,如果您想用您的格式描述年份,您必须使用 2006。因此您的格式必须是:
2006-01-02 15:04:05.000000000 -0700 MST
轻轻一点就可以了
package main
import (
"fmt"
"time"
)
func main() {
layout := "2006-01-02 15:04:05 -0700 MST"
t, _ := time.Parse(layout, "2019-09-19 04:03:01.770080087 +0000 UTC")
fmt.Println(t)
}
输出:
2019-09-19 04:03:01.770080087 +0000 UTC