自定义 time.Time 转 json 格式

This commit is contained in:
Shikong 2022-10-05 00:20:15 +08:00
parent 16a1217025
commit 9ba7ab03b0
2 changed files with 20 additions and 1 deletions

19
common/time/time.go Normal file
View File

@ -0,0 +1,19 @@
package time
import (
"fmt"
"time"
)
type Time time.Time
const (
timeFormat = "2006-01-02 15:04:05"
)
// MarshalJSON on JsonTime format Time field with %Y-%m-%d %H:%M:%S
func (t Time) MarshalJSON() ([]byte, error) {
// 重写time转换成json之后的格式
var tmp = fmt.Sprintf("\"%s\"", time.Time(t).Format(timeFormat))
return []byte(tmp), nil
}

View File

@ -1,7 +1,7 @@
package models
import (
"time"
"gofiber.study.skcks.cn/common/time"
)
type User struct {