Home > Mobile >  Compare time in Go [duplicate]
Compare time in Go [duplicate]

Time:10-07

I have a table contains column created_at I need to compare the time in this column with the now time for example:


result := database.DB.Where("code = ?", post.code).First(&post)

if result.CreatedAt < time.Now() {
        // do something
}

I got error in my editor: Invalid operation: result.CreatedAt > time.Now() (the operator > is not defined on Time)

How can check if the date expires?

CodePudding user response:

By using time.After or time.Before.

Playground example:

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")
    }
}
  • Related