Skip to content
Home » How to add given number of hours to the time in Golang

How to add given number of hours to the time in Golang

Learn about How to add given number of hours to the time 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 hours to the time in Golang

// Golang program to add given number of hours
// to the specified time

package main

import "fmt"
import "time"

func main() {
	ttime := time.Date(2020, 2, 5, 10, 5, 20, 0, time.UTC)

	res := ttime.Add(time.Hour * 8)
	fmt.Println("Time after 8 hours 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-time object using Date() function. Then we added 8 hours to the created date-time object 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 :
How to add given number of days to the specified date in Golang
Program on ParseDuration() function in Golang