栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

解组日期格式不正确的日期

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解组日期格式不正确的日期

您可以定义

time
支持两种格式的自己的字段类型:

type MyTime struct {    time.Time}func (self *MyTime) UnmarshalJSON(b []byte) (err error) {    s := string(b)    // Get rid of the quotes "" around the value.    // A second option would be to include them    // in the date format string instead, like so below:     //   time.Parse(`"`+time.RFC3339Nano+`"`, s)     s = s[1:len(s)-1]    t, err := time.Parse(time.RFC3339Nano, s)    if err != nil {        t, err = time.Parse("2006-01-02T15:04:05.999999999Z0700", s)    }    self.Time = t    return}type Test struct {    Time MyTime `json:"time"`}

[Try on Go Playground](https://play.golang.org/p/nYESNHgJI8)

在上面的示例中,我们采用预定义的格式

time.RFC3339Nano
,其定义如下:

RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"

并删除

:

"2006-01-02T15:04:05.999999999Z0700"

time.Parse
此处使用的这种时间格式在此处进行了描述:https : //golang.org/pkg/time/#pkg-
constants

另请参阅https://golang.org/pkg/time/#Parse的文档

time.Parse

PS

2006
在时间格式字符串中使用年份的事实可能是因为Golang的第一版于该年发布。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号