Standard library / time

time.Time.Round()

Round the instant to the nearest multiple of d since the zero time, ties upward; not local calendar boundaries. Nonpositive d strips monotonic data without rounding. Expected stdout: '12:35:00\n'.

Formal Syntax & Structure

go syntaxgo
func (t Time) Round(d Duration) Time

Parameters & Attributes

NameTypeDescription
dDurationElapsed duration or interval.

Return Value

Time

Round the instant to the nearest multiple of d since the zero time, ties upward; not local calendar boundaries.

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()            { fmt.Println(fixed().Round(time.Minute).Format(time.TimeOnly)) }
Go Reference