Menu Close

Go – Add hours, minutes, seconds to current time in Go

Here, we are going to learn about how to add hours, minutes, seconds to current time in Go. We will learn it by example and program.

Add() method of time structure will be used to add hours, minutes, seconds to current time.

Function prototype:

func (t Time) Add(d Duration) Time

Return value:

Add() function in time package returns the time t+d.

Example with code:

package main

import (

  "fmt"
  "time"
)

func main() {


  curr_time := time.Now()


  afterTenSeconds := curr_time.Add(time.Second * 10)

  afterTenMinutes := curr_time.Add(time.Minute * 10)

  afterTenHours := curr_time.Add(time.Hour * 10)

  afterTenDays := curr_time.Add(time.Hour * 24 * 10)


  fmt.Println("Current time: ", curr_time)

  fmt.Println("afterTenSeconds: ", afterTenSeconds)

  fmt.Println("afterTenMinutes: ", afterTenMinutes)

  fmt.Println("afterTenHours: ", afterTenHours)

  fmt.Println("afterTenDays: ", afterTenDays)

}

Output:

$ go run sample.go

Current time: 2020-05-31 11:51:30.381761 +0530 IST m=+0.000081402

afterTenSeconds: 2020-05-31 11:51:40.381761 +0530 IST m=+10.000081402

afterTenMinutes: 2020-05-31 12:01:30.381761 +0530 IST m=+600.000081402

afterTenHours: 2020-05-31 21:51:30.381761 +0530 IST m=+36000.000081402

afterTenDays: 2020-06-10 11:51:30.381761 +0530 IST m=+864000.000081402

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

https://www.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.