gorm time.time 时间加 和减
在 Gorm 中,可以使用 time.Time 类型来表示日期时间。可以使用 Add 方法将时间加上一个时间段,使用 Sub 方法将时间减去一个时间段。下面是一个示例:
import (
"time"
"github.com/jinzhu/gorm"
)
type User struct {
gorm.Model
Name string
CreatedAt time.Time
}
// 增加一个小时
user.CreatedAt = user.CreatedAt.Add(time.Hour)
// 减少一个小时
user.CreatedAt = user.CreatedAt.Sub(time.Hour)
在上面的示例中,使用 Add 方法将 CreatedAt 字段增加了一个小时,使用 Sub 方法将 CreatedAt 字段减少了一个小时。需要注意的是,Add 和 Sub 方法都会返回一个新的 time.Time 对象,因此需要将其赋值给原来的变量。
原文地址: https://www.cveoy.top/t/topic/wYY 著作权归作者所有。请勿转载和采集!