GO convert time.Duration to time.Time
To convert a time.Duration
to a time.Time
, you can use the Add()
method of the time.Time
type. The Add()
method allows you to add a duration to a given time and return the resulting time.
Here's an example of converting a time.Duration
to a time.Time
:
package main
import (
"fmt"
"time"
)
func main() {
duration := 2 * time.Hour // Example duration of 2 hours
// Getting current time
currentTime := time.Now()
// Adding the duration to the current time
newTime := currentTime.Add(duration)
fmt.Println("Current Time:", currentTime)
fmt.Println("New Time:", newTime)
}
In the above example, we start with a time.Duration
of 2 hours and then get the current time using time.Now()
. We then use the Add()
method to add the duration to the current time, and store the resulting time in the newTime
variable. Finally, we print both the current time and the new time to the console.
Please note that a time.Duration
represents a length of time, while a time.Time
represents a specific point in time. So when converting a duration to a time, the resulting time will depend on the reference point (e.g., the current time).