我有以下字符串2021-05-06 00:00:00 +0530 IST需要转换为 golang 中的 time.Time 值。我知道该怎么做,但我不知道解析这些类型的字符串的布局应该是什么。time, err := time.ParseInLocation("2021-05-06 00:00:00 +0530 IST", addedOn, loc)这给了我这样的错误"error":"parsing time \"2021-05-06 00:00:00 +0530 IST\" as \"2021-05-06 00:00:00 +0530 IST\": cannot parse \"-05-06 00:00:00 +0530 IST\" as \"1\""那么,这些字符串的正确布局应该是什么?
2 回答

www说
TA贡献1775条经验 获得超8个赞
您将日期放在时间布局的位置。
func ParseInLocation(layout, value string, loc *Location) (Time, error)
例如:
loc, _ := time.LoadLocation("Europe/Berlin")
// This will look for the name CEST in the Europe/Berlin time zone.
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.ParseInLocation(longForm, "Jul 9, 2012 at 5:02am (CEST)", loc)
fmt.Println(t)
在你的情况下:
t , _ := time.ParseInLocation("2006-01-02 15:04:05 -0700 MST", "2021-05-06 00:00:00 +0530 IST", loc)
请参阅操场示例(以及此处的其他ParseInLocation
示例)
- 2 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消