-5

I am trying to take a string and convert that string into ISO8601 timestamp format using Go.

I read a few StackOverflow posts and tried them, none of them worked for me. I might be doing it wrong?

My datetime string is something like

date := "8/16/2019 8:01:35 PM"
func main() {
    date := "8/16/2019 8:01:35 PM"
    t, _ := time.Parse("6/16/2019 3:07:53 PM", date)
    fmt.Println(t) // getting 0001-01-01 00:00:00 +0000 UTC
}

I am expecting to get a parsed DateTime value which is something like 2019-8-16T20:01:35 from the above code and then convert it to ISO8601 format, like 2019-8-16T20:01:35.000Z as an end result.

Dave C
  • 7,729
  • 4
  • 49
  • 65
Prady
  • 1
  • 2
  • 4
    First, read the documentation: [Package time](https://golang.org/pkg/time/) – peterSO Jul 30 '19 at 22:31
  • `"6/16/2019 3:07:53 PM"` is not a valid format string. A format string must format the canonical date, as clearly described in the docs. – Adrian Jul 30 '19 at 22:36
  • Also, never ignore errors. `time.Parse` returns an error and in your case it would have told you: parsing time "8/16/2019 8:01:35 PM" as "6/16/2019 3:07:53 PM": cannot parse "8/16/2019 8:01:35 PM" as "6/" – Dave C Jul 31 '19 at 12:15
  • Thanks all. I was just getting started with go and I did not understand much about how to look up for details in the documentation, my bad! – Prady Aug 02 '19 at 14:25

1 Answers1

1

the numbers on the example date can only be as follows

month: 1, 01, jan, January
day: 2, 02
year: 06, 2006,
hour: 3, 03, or for 24hour 15,
minutes: 4, 04
seconds 5, 05, 05.000, 05.000000
timezone -0700, MST

input := "8/16/2019 8:01:35 PM"
layout := "1/02/2006 3:04:05 PM"
t, err := time.Parse(layout, input)
fmt.Println(t, err)
fmt.Println(t.Format("1/02/2006 3:04:05 PM")) // 31-Aug-2017

go playground

for more details checkout

Dave C
  • 7,729
  • 4
  • 49
  • 65
Isaac Weingarten
  • 977
  • 10
  • 15