tonytonytony
2021-07-10 14:12:17
go格式化日期
项目描述
go 格式日期算法
运行环境
goland
项目技术(必填)
golang
是否原创(转载必填原文地址)
原创
项目截图(必填)
运行截图(必填)
代码片段
package main
import (
"encoding/json"
"fmt"
"time"
)
type Time time.Time
const (
timeFormart = "2006-01-02 15:04:05"
)
func (t *Time) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local)
*t = Time(now)
return
}
func (t Time) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(timeFormart)+2)
b = append(b, '"')
b = time.Time(t).AppendFormat(b, timeFormart)
b = append(b, '"')
return b, nil
}
func (t Time) String() string {
return time.Time(t).Format(timeFormart)
}
type Person struct {
Id int64 `json:"id"`
Name string `json:"name"`
Birthday Time `json:"birthday"`
}
func TestTimeJson() {
now := Time(time.Now())
fmt.Println(now)
src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30 16:09:51"}`
p := new(Person)
err := json.Unmarshal([]byte(src), p)
if err != nil {
fmt.Println(err)
}
fmt.Println(p)
fmt.Println(time.Time(p.Birthday))
js, _ := json.Marshal(p)
fmt.Println(string(js))
}
func main() {
TestTimeJson()
}
猜你喜欢
- golang数组排序
- golang读取excel文件生成SQL语句
- golang实现简单的udp协议服务端与客户端示例
- Flex xml格式化以及缩进量
- xmlformat.js实现XML格式化和压缩
- 本地html版本的json格式化工具,提供两种试图,可左右适配切换,功能强大
- HTML+CSS+JS代码格式化工具
- PhoneGap+HTML5+jqueryMobile 开发安卓-DATE控件
- java时间工具类(自定义java时间函数30多个)
- python七段数码管绘制时间
- javascript网页日期时间插件
- js可选择时分秒的日历时间控件代码下载,多浏览器IE8,9,10、谷歌chrome,火狐firefox等都是兼容的
请下载代码后再发表评论
最近下载
最近浏览
dengge123 LV14
2024年7月19日
2206371875 LV7
2023年12月5日
只要有你 LV8
2021年12月3日
1515997875
2021年9月26日
暂无贡献等级
CrystalQ LV8
2021年8月22日
最代码_码鑫源 LV6
2021年7月30日
terryang LV19
2021年7月20日
bk468822
2021年7月17日
暂无贡献等级
interface LV22
2021年7月15日
情绪吖 LV3
2021年7月13日




