gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:31:21 25 4
gpt4 key购买 nike

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

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())

最佳答案

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

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

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

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

关于go - 字符串到日期的转换(带有 +0530 的 IST),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52445357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com