Standard library / time

time.Time.Compare()

Return -1, 0 or +1 according to whether t is before, equal to or after u, using monotonic readings when both have them. Expected stdout: '-1 0 1\n'.

Formal Syntax & Structure

go syntaxgo
func (t Time) Compare(u Time) int

Parameters & Attributes

NameTypeDescription
uTimeOther instant for comparison or subtraction.

Return Value

int

Return -1, 0 or +1 according to whether t is before, equal to or after u, using monotonic readings when both have them.

Working Example

Example Snippetgo
package main
import (  "fmt"  "time")
func fixed() time.Time { return time.Date(2024, 2, 29, 12, 34, 56, 123000000, time.UTC) }func main() {  t := fixed()  fmt.Println(t.Compare(t.Add(time.Second)), t.Compare(t), t.Compare(t.Add(-time.Second)))}
Go Reference