Time Date Normalizes Overflow Fields

Build dates from out-of-range fields and let time.Date normalize them, turning day 0, month 14, or a negative nanosecond offset into real instants.

GoData Integrity

Code

snippet
package main

import (
	"fmt"
	"time"
)

func main() {
	// time.Date normalizes overflow fields instead of failing
	rollover := time.Date(2026, time.March, 0, 12, 0, 0, 0, time.UTC)
	fmt.Println("day 0:", rollover.Format("2006-01-02"))

	month14 := time.Date(2026, time.Month(14), 1, 0, 0, 0, 0, time.UTC)
	fmt.Println("month 14:", month14.Format("2006-01-02"))

	lateHour := time.Date(2026, time.January, 2, 25, 61, 0, 0, time.UTC)
	fmt.Println("hour 25 minute 61:", lateHour.Format("2006-01-02 15:04"))

	// negative nanoseconds move the instant backwards
	negative := time.Date(2026, time.January, 1, 0, 0, 0, -1, time.UTC)
	fmt.Println("nanos -1:", negative.Format("2006-01-02 15:04:05.999999999"))

	// day 0 of a month is the last day of the previous month
	start := time.Date(2026, time.February, 1, 0, 0, 0, 0, time.UTC)
	fmt.Println("jan end:", start.AddDate(0, 0, -1).Format("2006-01-02"))
}

How it works

Everything happens inside the function main.

What to watch for

  • Runs on the standard library alone; nothing to install.

Using it

The primary signature, spelled out, is func main()

The demo output is labeled (day 0:, month 14:, hour 25 minute 61:), so each printed line points back to the operation that produced it.

Key statements

lateHour := time.Date(2026, time.January, 2, 25, 61, 0, 0, time.UTC)

Interactive Tools for This Topic

FAQ

What does Time Date Normalizes Overflow Fields print when run?
Invoke the file as-is — the `day 0:`, `month 14:`, `hour 25 minute 61:` prefixes identify which step printed each line.