-3

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
AlleyOOP
  • 1,536
  • 4
  • 20
  • 43

3 Answers3

1

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
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
0

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

Community
  • 1
  • 1
dave
  • 62,300
  • 5
  • 72
  • 93
-1

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

AlleyOOP
  • 1,536
  • 4
  • 20
  • 43