将“2021-05-06 00:00:00 +0530 IST”之类的字符串转换为

我有以下字符串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\""

那么,这些字符串的正确布局应该是什么?


互换的青春
浏览 125回答 2
2回答

www说

您将日期放在时间布局的位置。看time#ParseInLocationfunc 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示例)

慕丝7291255

布局👇🏻"2006-01-02 15:04:05 -0700 MST"查看文档和示例PLAYGROUND
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go