Skip to main content

日期

举个例子 只用看 Addtime

type Article struct {
Model

TagID int `json:"tag_id" gorm:"index"`
Tag Tag `json:"tag"`

Title string `json:"title"`
Desc string `json:"desc"`
Content string `json:"content"`
CreatedBy string `json:"created_by"`
ModifiedBy string `json:"modified_by"`
State int `json:"state"`
Addtime string `json:"add_time" gorm:"column:add_time;index"`
}




func GetArticles(pageNum int, pageSize int, maps interface{}) (articles []Article) {
db.Preload("Tag").Where(maps).Offset(pageNum).Limit(pageSize).Find(&articles)

for i, v := range articles {
// res_time, _ := time.Parse("2006-01-02T15:04:05Z07:00", strconv.Itoa(v.Model.CreatedOn*1000))
// v.Addtime = res_time.Format("2006-01-02 15:04:05")
// v.Addtime = (res_time)
fmt.Println("ddddd---", i, v)
// res_time := time.Unix(int64(v.Model.CreatedOn), 0)
res_time, _ := time.Parse("2006-01-02T15:04:05Z07:00", v.Addtime)
articles[i].Addtime = res_time.Format("2006-01-02 15:04:05")
}

// fmt.Print("sssss---", articles)

return articles
}

日期定义 gorm

    Addtime    string `json:"add_time" gorm:"column:add_time;index"`

数据中格式转换

    res_time, _ := time.Parse("2006-01-02T15:04:05Z07:00", v.Addtime)
articles[i].Addtime = res_time.Format("2006-01-02 15:04:05")

如果存储的是int类型

    res_time := time.Unix(int64(v.Model.CreatedOn), 0)
articles[i].Addtime = res_time.Format("2006-01-02 15:04:05")

日期例子

func main() {
new := time.Now()
fmt.Println(new)
fmt.Println(new.Unix())
fmt.Println(new.UnixNano())
fmt.Println(time.Unix(new.Unix(), 0))
// 定时器
// aa := time.Tick(time.Second)
// for t := range aa {
// fmt.Println(t.Format("2006-01-02 03:04:05"))
// }
// dd, _ := time.Parse("2006-01-02 03:04:05", "2022-01-03 03:04:05")
dd, _ := time.Parse("2006-01-02 03:04:05", "1666670424407555900")

fmt.Println(dd)
}

结果

2022-10-25 12:30:01.8779719 +0800 CST m=+0.010101201
1666672201
1666672201877971900
2022-10-25 12:30:01 +0800 CST
0001-01-01 00:00:00 +0000 UTC