字符串到日期的转换(带+0530的IST)

时间:2018-09-21 13:58:32

标签: go

我无法将字符串"2017-12-07T20:01:33+0530"转换为日期格式。我正在使用 RFC3339 RFC3339Nano ,但仍收到以下错误:

0001-01-01 00:00:00 +0000 UTC parsing time "2016-01-17 20:04:05 +0530": hour out of range
IST to UTC: 0001-01-01 00:00:00 +0000 UTC

这是我的代码:

IST, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
    fmt.Println(err)
    return
}

const longForm = "2006-01-02 15:04:05 +0530"
t, err := time.ParseInLocation(longForm, "2016-01-17 20:04:05 +0530", IST)
fmt.Println(t, err)
fmt.Printf("IST to UTC: %v\n\n", t.UTC())

1 个答案:

答案 0 :(得分:3)

时区的格式说明符错误;你有:

const longForm = "2006-01-02 15:04:05 +0530"

但是时区定义为-0700,而不是+0530。所以应该是:

const longForm = "2006-01-02 15:04:05 -0700"