-3

Simple code:

func main() {
    date := "2020-12-23T16:39:24.362+06:00"
    t, _ := time.Parse("2006-01-02T15:04:05.000+06:00", date)
    fmt.Printf("t = %s", t)
}

Result is t = 2006-12-23 16:39:24.362 +0000 UTC

Link to playground: https://play.golang.org/p/3U6CzIrrMsM

Where I made a mistake?

1
  • 1
    You can use a predefined format; t, _ := time.Parse(time.RFC3339, date) - docs Dec 23, 2020 at 13:14

1 Answer 1

3

The reference time has -0700 time zone. Quoting from time.Parse():

Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be

Mon Jan 2 15:04:05 -0700 MST 2006

would be interpreted if it were the value; it serves as an example of the input format. The same interpretation will then be made to the input string.

With this change it works:

date := "2020-12-23T16:39:24.362+06:00"

t, err := time.Parse("2006-01-02T15:04:05.000-07:00", date)
fmt.Println(err)
fmt.Printf("t = %s", t)

And outputs (try it on the Go Playground):

<nil>
t = 2020-12-23 16:39:24.362 +0600 +0600

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.