Use time.ParseDuration()
https://go.dev/play/p/SWUHBiSpflh
On Sunday, 3 April 2022 at 23:48:54 UTC+1 vika...@gmail.com wrote:
> I am looking to convert a *string* (say 4) to type *time.Duration*.
>
> I've looked around but could not find a way to do so.
>
> // https://go.dev/play/p/EUuDAY-Qx8N
>
func addHours(t time.Time, hours string) (time.Time, error) {
i, err := strconv.ParseInt(hours, 10, 64)
if err != nil {
return time.Time{}, err
}
return t.Add(time.Hour * time.Duration(i)), nil
}
https://go.dev/play/p/jjLO54tbJn4
Peter
On Sunday, April 3, 2022 at 6:48:54