我有以下字串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\""
那么,這些字串的正確布局應該是什么?
uj5u.com熱心網友回復:
您將日期放在時間布局的位置。
看 time#ParseInLocation
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此處的其他示例)
uj5u.com熱心網友回復:
布局????
"2006-01-02 15:04:05 -0700 MST"
查看包檔案
PLAYGROUND
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/352537.html
下一篇:父context.Context與gin.Contect.Request.Context與gin.Context
