为了账号安全,请及时绑定邮箱和手机立即绑定

在 Go 中比较时间

在 Go 中比较时间

Go
弑天下 2022-10-04 19:13:50
我有一个表格包含列,我需要将此列中的时间与现在的时间进行比较,例如:created_atresult := database.DB.Where("code = ?", post.code).First(&post)if result.CreatedAt < time.Now() {        // do something}我在编辑器中遇到错误:Invalid operation: result.CreatedAt > time.Now() (the operator > is not defined on Time)如何检查日期是否过期?
查看完整描述

1 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

通过使用时间。之后时间。之前

游乐场示例:

package main


import (

    "fmt"

    "time"

)


func main() {

    created := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)

    now := time.Now()

    if created.Before(now) {

        fmt.Println("do something")

    }

}


查看完整回答
反对 回复 2022-10-04
  • 1 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信