doutang2382 2014-06-25 06:45
浏览 272
已采纳

time.parse:golang为什么会错误地解析时间?

I'm trying to parse a string as time with but unfortunately go gets the wrong month ( January instead of June)

package main

import "fmt"
import "time"

func main() {

    t := "2014-06-23T20:29:39.688+01:00"
    tc, _ := time.Parse("2006-01-02T15:04:05.000+01:00", t)

    fmt.Printf("t was %v and tc was %v", t, tc)

}

Play

  • 写回答

2条回答 默认 最新

  • drne47241 2014-06-25 07:00
    关注

    The problem is that your timezone offset is ill-defined in the layout: the reference offset is -0700. You defined yours as +01:00, so the 01 is interpreted as the month and erase the previously defined one. And as your working offset is 01 as well, it is parsed as january.

    The following example works for me playground

    package main
    
    import "fmt"
    import "time"
    
    func main() {
    
        t := "2014-06-23T20:29:39.688+01:00"
        tc, _ := time.Parse("2006-01-02T15:04:05.000-07:00", t)
    
        fmt.Printf("t was %v and tc was %v", t, tc)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料