-3

I need to convert a GMT date like (Mon, 23 Dec 2019 18:52:45 GMT) to time.Time.Unix in Go

I've tried to parse it before and then convert it to Unix format. But it's not so clean what are the predefined layouts in time.Parse https://golang.org/pkg/time/#Parse

How to do that?

Alejandro Blasco
  • 1,295
  • 2
  • 20
  • 24

2 Answers2

1

That looks like time.RFC1123. If not, refer to the same section to create your own layout reference string.

Jesse Amano
  • 800
  • 5
  • 16
1

The layout of the date is RFC1123. Parse it with time.Parse() then convert it to Unix with time.Time.Unix().

t, _ := time.Parse(time.RFC1123, "Mon, 23 Dec 2019 18:52:45 GMT")
tUnix:= t.Unix()
fmt.Printf("%s in Unix is %d",t,tUnix)
// Output: 2019-12-23 18:52:45 +0000 GMT in Unix is 1577127165

Try it online

FuXiang Shu
  • 100
  • 8