Menu Close

Go – Add years, months, days to current date in Go

Here, we are going to learn about how to add years, months, days to current date in Go. We will learn it by example and program.

Function prototype:

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

The AddDate function takes three arguments: the year, month, and day to add to the original date. These values can be positive or negative, indicating whether the date should be advanced or moved back in time. The function returns a new Time value with the added date.

Return:

AddDate() function in time package returns the time corresponding to adding the given number of years, months, and days to t.

Example:

Date: February 1, 2019

Apply: AddDate(1, 2, 3)

Here, Going to add 1 year, 2 months and 3 days in the given date.

Final date: April 4, 2020

Example 1:

package main

import (
	"fmt"
	"time"
)

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

	// Add 2 years, 3 months, and 5 days to the current date and time
	futureDate := now.AddDate(2, 3, 5)

	// Subtract 1 year, 2 months, and 3 days from the current date and time
	pastDate := now.AddDate(-1, -2, -3)

	// Print the current date and time, future date, and past date
	fmt.Println("Current Date and Time:", now)
	fmt.Println("Future Date:", futureDate)
	fmt.Println("Past Date:", pastDate)
}

In this example, we first import the fmt and time packages. We then get the current date and time using the Now function of the time package, and store it in the now variable.

Next, we use the AddDate function to add 2 years, 3 months, and 5 days to the current date and time, and store the resulting Time value in the futureDate variable.

We then use the AddDate function again to subtract 1 year, 2 months, and 3 days from the current date and time, and store the resulting Time value in the pastDate variable.

Finally, we print the current date and time, future date, and past date using the Println function of the fmt package.

When you run this program, you should see output similar to the following:

Output:

Current Date and Time: 2023-03-19 11:25:12.2751429 +0530 IST m=+0.006967201
Future Date: 2025-06-24 11:25:12.2751429 +0530 IST m=+83016002.006967201
Past Date: 2022-01-16 11:25:12.2751429 +0530 IST m=-41524707.993032799

As you can see, the AddDate function has correctly added 2 years, 3 months, and 5 days to the current date and time to calculate the future date, and subtracted 1 year, 2 months, and 3 days from the current date and time to calculate the past date.

Example 2:

package main

import (

  "fmt"
  "time"
)

func main() {

  curr_date := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC)

  oneDayLater := curr_date.AddDate(0, 0, 1)
  oneMonthLater := curr_date.AddDate(0, 1, 0)
  oneYearLater := curr_date.AddDate(1, 0, 0)
  oneYearBack := curr_date.AddDate(-1, 0, 0)


  fmt.Println("Current date: ", curr_date)
  fmt.Println("oneDayLater: ", oneDayLater)
  fmt.Println("oneMonthLater: ", oneMonthLater)
  fmt.Println("oneYearLater: ", oneYearLater)
  fmt.Println("oneYearBack: ", oneYearBack)

}

Output:

Current date: 2010-01-01 00:00:00 +0000 UTC
oneDayLater: 2010-01-02 00:00:00 +0000 UTC
oneMonthLater: 2010-02-01 00:00:00 +0000 UTC
oneYearLater: 2011-01-01 00:00:00 +0000 UTC
oneYearBack: 2009-01-01 00:00:00 +0000 UTC

To learn more about golang, Please refer given below link:

https://techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, time

Leave a Reply

Your email address will not be published. Required fields are marked *