112
votes

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.

2
golang.org/pkg/time/#Time.AddDate AddDate() function added years, months and days to timesunkuet02

2 Answers

171
votes

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))
8
votes

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