Alaister Young
Alaister Young

Reputation: 357

Parsing javascript date to a golang date

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.

Upvotes: 3

Views: 7170

Answers (1)

Alexey Soshin
Alexey Soshin

Reputation: 17711

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

Example

Upvotes: 3

Related Questions