GORM對實現datetime和date類型時間


model 定義

type Plan struct {
	Id int `gorm:"primary_key"`
	Title string `gorm:"column:title;not null;size:128"`
	Start time.Time  `gorm:"type:date;column:start"`
	End time.Time  `gorm:"type:date;column:end"`
	Created   time.Time `gorm:"autoCreateTime;column:created;type:datetime"`
	IsDelete int `gorm:"column:is_delete;default:1"`
}

  相比官網的例子,增加了type屬性

執行:

	db.AutoMigrate(&Plan{})

 

結果:

 

 建議把類型改為string

type Plan struct {
	Id int `gorm:"primary_key"`
	User int `gorm:"column:user;not null"`
	Title string `gorm:"column:title;not null;size:128"`
	Start string `gorm:"type:date;column:start"`
	End string `gorm:"type:date;column:end"`
	Created   time.Time `gorm:"autoCreateTime;column:created;type:datetime"`
	IsDelete int `gorm:"column:is_delete;default:1"`
}

  這樣gin處理前端發過來的時間比較友好,不然會出現這樣的報錯

parsing time "2019-09-06" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "T"

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM