1

I am executing below code to parse a time

var time_format = "2006-01-02T15:04:05.000+0700"
var s = "2018-08-23T14:10:31.692+0700"
p, _ := time.Parse(time_format, s)
fmt.Println(p.String())

The output of above program is as below.

2018-08-23 14:10:31.692 +0000 UTC

It is the same time in UTC while I am parsing a time which is +0700 ahead of UTC so as expeceted result should be

2018-08-23 7:10:31.692 +0000 UTC

Can anyone tell what is the issue here.

0

1 Answer 1

5

It's because your format string is not correct. The timezone indication must be -0700 (not +0700). time.Parse():

The layout defines the format by showing how the reference time, defined to be

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

With that change it works:

var format = "2006-01-02T15:04:05.000-0700"

var s = "2018-08-23T14:10:31.692+0700"
p, err := time.Parse(format, s)

fmt.Println(p.String(), err)

This will output (try it on the Go Playground):

2018-08-23 14:10:31.692 +0700 +0700 <nil>
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.