1 回答

TA貢獻1828條經驗 獲得超3個贊
func HandleTime(c *gin.Context) {
type User struct {
CreatedAt time.Time `json:"created_at" binding:"required" time_format:"2006-01-02T15:04:05Z07:00"`
}
var user User
fmt.Println(user.CreatedAt.String())
if err := c.ShouldBindJSON(&user); err != nil {
fmt.Println(err)
return
}
c.JSON(200, gin.H{
"created": user.CreatedAt.String(),
})
}
curl -X POST 'http://127.0.0.1:8092/${YOUR_PATH}' \
-H 'Content-Type: application/json' -d '{
"created_at": "2019-01-01T01:00:09+08:00"
}'
回復:
{
"created": "2019-01-01 01:00:09 +0800 CST"
}
在 go 文檔中查看:https://pkg.go.dev/[email protected]#example-Parse
例如,RFC3339布局 2006-01-02T15:04:05Z07:00 包含 Z 和時區偏移量,以便處理兩個有效選項:
2006-01-02T15:04:05Z
2006-01-02T15:04:05+07:00。
- 1 回答
- 0 關注
- 141 瀏覽
添加回答
舉報