dsnm64969 2018-08-23 11:36
浏览 52
已采纳

Golang时间解析问题

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.

  • 写回答

1条回答 默认 最新

  • douzhuangna6906 2018-08-23 11:42
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!