3

I need to parse a JavaScript formatted date which I obtain from calling new Date() and looks like Sat Aug 27 2016 17:07:43 GMT+1000 (AEST).

I am then posting this as a string go my golang server where I need to parse it to be formatted the same as when calling time.Now() which looks like 2016-08-30 14:05:31.563336601 +1000 AEST. This date is then stored in my database via gorm which is why I believe it needs to be in this format.

What is the best way of doing this?

Thanks.

Alaister Young
  • 357
  • 3
  • 20
  • Use `+new Date()` to get a timestamp and feed that into something like this: http://stackoverflow.com/questions/24987131/how-to-parse-unix-timestamp-in-golang – Rob M. Aug 27 '16 at 07:18
  • Hi @RobM. This is a good idea but after inserting it into the db it looks like this: `48624-10-12 20:40:21+11` – Alaister Young Aug 27 '16 at 07:37
  • 1
    Did you try to use the javascript date method *toISOString()*? Maybe it format the date in the way you need. If you work on timestamps, pay attention that javascript produce the time in milliseconds, the c standard is in seconds (in golang Unix) and go lang support even nanoseconds. So you have to check which type of conversion you're doing. – Mario Santini Aug 27 '16 at 08:10

1 Answers1

3

This should give you the correct date. Note how you specify the format:

jsTime, err := time.Parse("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)", "Sat Aug 27 2016 17:07:43 GMT+1000 (AEST)")

if (err != nil) {
    fmt.Printf("Error %v\n", err)
    return
}

fmt.Println(jsTime.Format("2006-01-02 15:04:05.000000000 -0700 MST"))
Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40