将小时,分钟,秒添加到当前时间


95

如何将小时,分钟和秒(定义为整数)添加到当前时间,类似于AddDate

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

但要花几小时,几分钟和几秒钟


2
golang.org/pkg/time/#Time.AddDate ADDDATE()函数添加年,月,日,以时间
sunkuet02

Answers:


145

我猜你在找什么

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

感谢您的快速回复!
joshii_h 2016年

那是什么==符号?
vishnuprasanth

1
用于同时声明和分配变量的语法糖。替换var timein time。时间后接timein = ...
弗兰克·

10
简化:timein := time.Now().Add(time.Hour * hours + time.Minute * mins + time.Second * sec)
Trevin Avery

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.