Skip to content
Home » How to add given number of days to the specified date in Golang

How to add given number of days to the specified date in Golang

Learn about How to add given number of days to the specified date in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

How to add given number of days to the specified date in Golang

// Golang program to add given number of days
// to the specified date

package main

import "fmt"
import "time"

func main() {
	date := time.Date(2022, 3, 4, 10, 5, 20, 0, time.UTC)

	res := date.Add(time.Hour * 24 * 10)
	fmt.Println("date after 15 days is: ", res)
}

In the above program, we imported the fmt package, which contains the files of the fmt package, and then we can use a function related to the fmt package. Here, we created a date object using Date() function. Then we added 10 days to the date using Add() function.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Program on ParseDuration() function in Golang
Program to get the date, month, year using Date() function in Golang