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.

3
  • Use +new Date() to get a timestamp and feed that into something like this: stackoverflow.com/questions/24987131/… Commented Aug 27, 2016 at 7: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 Commented Aug 27, 2016 at 7: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. Commented Aug 27, 2016 at 8:10

1 Answer 1

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

Example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.