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.
Code
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
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
- How do you run Iterate A Date Range With AddDate?
- Direct execution is enough; the script has no external requirements.
Report a problem with this exampleOpens a pre-filled issue on GitHub.