-4

I want to check if time.Now is after another time.Time in Go.

person.CreatedAt is time.Time

if time.Now > person.CreatedAt {
    fmt.Println("time.Now is after person.CreatedAt")
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Janet
  • 7
  • 2

2 Answers2

1

You can use time.After, time.Before and time.Equal to compare times:

if time.Now().After(person.CreatedAt) {
    fmt.Println("time.Now is after person.CreatedAt")
}

To check if a time.Time variable is empty use time.IsZero

TehSphinX
  • 6,536
  • 1
  • 24
  • 34
1

Here simple example how you can check it:

package main

import (
    "fmt"
    "time"
)

func main() {
    dateFormat := "2006-01-02"
    personCreatedAt, err := time.Parse(dateFormat, "2020-01-01")
    if err != nil {
        // error handling...
    }
    ok := time.Now().After(personCreatedAt)
    fmt.Println(ok)
}

Result will be: true

cn007b
  • 16,596
  • 7
  • 59
  • 74