Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding hours, minutes, seconds to current time

Tags:

time

go

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.

like image 390
joshii_h Avatar asked Nov 14 '16 12:11

joshii_h


People also ask

How do I add hours minutes seconds increments in Excel?

Enter formula =A1+TIME(0,20,0) into the Formula Bar, and then press the Ctrl + Enter key simultaneously. You can see each cell time is added with 20 minutes increments and listed in selected range immediately.

How do you add time?

To add time, you add the hours together, then you add the minutes together. Because there are only 60 minutes in an hour, you cannot have a time whose minute value is greater than 60. In this case, subtract 60 minutes and add 1 more to the hour.


2 Answers

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))
like image 178
Franck Jeannin Avatar answered Oct 16 '22 17:10

Franck Jeannin


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
like image 7
heemayl Avatar answered Oct 16 '22 17:10

heemayl