简体   繁体   中英

Golang Time parse issue

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM