Library contents

Go Time.AddDate

Advance a calendar date by two days to set a delivery deadline.

A simple example

Time.AddDate

  1. Start with 2024-01-31
  2. Do this Add one calendar day
  3. Result 2024-02-01
January ends on day 31. The next day starts a new month.

Syntax

Go 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.go

Calculate 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.

Result2024-03-01
main.go
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"))
}
Output
2024-03-01

The day after February 28 is February 29 in 2024. One more day brings the date to March 1.

Advertisements

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?

Given28 February 2024 + 2 days
Worked result2024-03-01

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?

Given31 December 2026 + 1 day
Worked result2027-01-01

What to watch for

Calendar days include weekends. A business-day promise needs a separate holiday and weekend policy.

Documentation for Time.AddDate