如何解析“ 2019-09-19 04:03:01.770080087 +0000 UTC”时间戳[重复]

This question already has an answer here:

How would I parse this timestamp?

"2019-09-19 04:03:01.770080087 +0000 UTC"

I've tried the following:

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)

But all I get as output is:

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

</div>

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"

https://play.golang.org/p/APkXHUAhMQ3

The time format you pass to parse is not an "example" format. Each time field has a distinct value:

Mon Jan 2 15:04:05 -0700 MST 2006

For instance, if you want to describe the year in your format, you have to use 2006. So your format must be:

2006-01-02 15:04:05.000000000 -0700 MST

A little dab'll do ya

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)
}

Outputs:

2019-09-19 04:03:01.770080087 +0000 UTC