为什么time.Parse解析时间不正确? [英] why does time.Parse parse the time incorrectly?

查看:74
本文介绍了为什么time.Parse解析时间不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析字符串作为时间,但很不幸错误的月份(1月而不是6月)

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)

}

播放

推荐答案

问题是您的时区偏移在布局中定义不正确:参考偏移为 -0700 .您将其定义为 +01:00 ,因此将 01 解释为月份,并删除先前定义的月份.而且由于您的工作偏移也是 01 ,因此它也被解析为一月.

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.

以下示例适用于我游乐场

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)

}

这篇关于为什么time.Parse解析时间不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆