Adding hours, minutes, seconds to current time

GoTime

Go Problem Overview


How do I add hours, minutes, and seconds (defined as ints) to the current time, similar to AddDate?

timein := time.Now().Local().AddDate(Hours, Mins, Sec)

but with hours, minutes, and seconds.

Go Solutions


Solution 1 - Go

I guess what you are looking for is

timein := time.Now().Local().Add(time.Hour * time.Duration(Hours) +
                                 time.Minute * time.Duration(Mins) +
                                 time.Second * time.Duration(Sec))

Solution 2 - Go

This answer is outdated. Please see this answer.


AddDate takes (and adds) year, month, day as parameters, not hour, minute, second.

From https://golang.org/pkg/time/#Time.AddDate:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjoshii_hView Question on Stackoverflow
Solution 1 - GoFranck JeanninView Answer on Stackoverflow
Solution 2 - GoheemaylView Answer on Stackoverflow