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.
Code
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
Age CalculatorCalculate exact age in years, months, and days between a birth date and a reference date, plus total days and the next birthday countdown.Business Days CalculatorCount the weekdays between two dates, excluding weekends and any holidays you list, and see how many calendar and weekend days are involved.Cron Next Runs CalculatorCompute the next scheduled run times for a standard five-field cron expression, supporting ranges, lists, steps, and day-of-week naming semantics.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.