本文摘自php中文网,作者藏色散人,侵删。
下面由Golang语言教程栏目给大家介绍Golang结构体json的时间格式化解决办法,希望对需要的朋友有所帮助!

1 2 3 | 今天开发的过程中遇到一个问题,数据库内有个日期字段(类型: date )查询出来后会是
2021-01-01T20:08:23.000000028+08:00 这种格式,而我只要2021-01-01,
下面列出解决办法
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | type JsonTime time.Time
func (this JsonTime) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf( "\"%s\"" , time.Time(this).Format( "2006-01-02" ))
return []byte(stamp), nil
}
type Workday struct {
Id int
Date JsonTime
}
数据获取好后直接解析就可以了
d1, _ := json.Marshal(work. Date )
fmt.Printf( "%s" ,d1)
现在d1 就是Y-m-d格式了
|
以上就是解决Golang结构体json的时间格式化的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
golang的多态
golang 定时器详解
golang 如何类型转换
deepin下配置protobuf(含go语言protoc-gen-go插件安装)
golang能写人工智能吗
在vscode中使用 gopls
golang怎么搭一个网站
windows下如何玩转火热的go-zero
总结air在go的其他版本上运行可能遇到的问题
专访go语言布道师dave cheney:go语言这十年,只能用“成功”一词总结
更多相关阅读请进入《golang》频道 >>
老貘
一个与时俱进的Go编程知识库。
转载请注明出处:木庄网络博客 » 解决Golang结构体json的时间格式化