Iterate A Date Range With AddDate

AddDate steps one calendar day at a time, so a for loop covers a date range cleanly even when it crosses a month boundary.

GoData Integrity

Code

snippet
package main

import (
	"fmt"
	"time"
)

func main() {
	start := time.Date(2026, time.November, 28, 0, 0, 0, 0, time.UTC)
	end := time.Date(2026, time.December, 2, 0, 0, 0, 0, time.UTC)

	// iterate day by day: AddDate steps one calendar day, so the loop
	// crosses the month boundary without manual month arithmetic
	for day := start; !day.After(end); day = day.AddDate(0, 0, 1) {
		fmt.Println(day.Format("2006-01-02"))
	}
}

How the snippet works

Under the hood: the function main.

The loop for day := start; !day.After(end); day = day.AddDate(0, 0, 1) is the only repeated construct.

Points of care

  • Built-in APIs only — no supply chain needs to be vetted.

Using it

Entry signature: func main()

The key statements, annotated

start := time.Date(2026, time.November, 28, 0, 0, 0, 0, time.UTC)

Interactive Tools for This Topic

FAQ

How do you run Iterate A Date Range With AddDate?
Direct execution is enough; the script has no external requirements.