Go Time.AddDate
Advance a calendar date by two days to set a delivery deadline.
A simple example
Time.AddDate
- Start with 2024-01-31
- Do this Add one calendar day
- Result 2024-02-01
Syntax
arrival.AddDate(0, 0, 2).Format("2006-01-02")
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goCalculate a delivery date across leap day
A reservation spans the end of February in a leap year. The booking system must use actual calendar days so that the extra day appears in the schedule or stay length.
package main
import (
"fmt"
"time"
)
func main() {
arrival := time.Date(2024, 2, 28, 0, 0, 0, 0, time.UTC)
fmt.Println(arrival.AddDate(0, 0, 2).Format("2006-01-02"))
}
2024-03-01
The day after February 28 is February 29 in 2024. One more day brings the date to March 1.
Return a borrowed microscope
A school lends a microscope two days before the end of February in a leap year. Parse the stated local calendar date and add the indicated number of days. Which date goes on the reminder?
Send a year-end reminder
A gallery schedules its reminder one day after the final day of the year. Parse the stated local calendar date and add the indicated number of days. Which date goes on the reminder?
What to watch for
Calendar days include weekends. A business-day promise needs a separate holiday and weekend policy.