Standard library / time

time.Time.Sub()

Return t minus u as a duration, saturating at representable limits. Use monotonic readings when both are present. Expected stdout: '1h30m0s\n'.

Formal Syntax & Structure

go syntaxgo
func (t Time) Sub(u Time) Duration

Parameters & Attributes

NameTypeDescription
uTimeOther instant for comparison or subtraction.

Return Value

Duration

Return t minus u as a duration, saturating at representable limits.

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.Add(90 * time.Minute).Sub(t)) }
Go Reference