How to Use time.Time.AddDate() Function in Golang

Golang time.Time.AddDate() function is “used to check the time which is equivalent to adding the stated number of years, months, and days to the given t.”

To add a day to date in Golang, you can use the “time.Time.AddDate()” function.

Syntax

func (t Time) AddDate(years int, months int, days int) Time

Parameters

t: It is the stated time.

Return value

The AddDate() function returns the result of adding the stated t to the stated number of years, months, and days.

Example: How to Use AddDate() function in Go

package main

import (
  "fmt"
  "time"
)

func main() {
  // Get the current time
  now := time.Now()

  // Get the time 2 years, 3 months, and 4 days from now
  future := now.AddDate(2, 3, 4)

  fmt.Println("Current Time:", now)
  fmt.Println("Future Time:", future)
}

Output

Current Time: 2023-07-01 19:35:57.031539 +0530 IST m=+0.000233418

Future Time: 2025-10-05 19:35:57.031539 +0530 IST

How to Add a Month to a date in Go (Golang)

To add a month to a date in Golang, use the “time.Time.AddDate()” function.

package main

import (
  "fmt"
  "time"
)

func main() {
  // Get the current time
  now := time.Now()

  // Add one month to the current time
  oneMonthLater := now.AddDate(0, 1, 0) 

  fmt.Println("Current date:", now)
  fmt.Println("One month later:", oneMonthLater)
}

Output

Current date: 2023-07-01 19:42:34.947149 +0530 IST m=+0.000285293
One month later: 2023-08-01 19:42:34.947149 +0530 IST

That’s it.