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 PM UTC-4 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 > package main > > import ( > "fmt" > "time" > ) > > func main() { > // var addHours string = 4 > fmt.Println(time.Now().Local()) > timeAdd := time.Now().Local().Add(time.Hour * 4) // I want to use 4 > from a string variable here > fmt.Println(timeAdd) > } > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ff90b3e9-683d-4c1d-8de5-161483298633n%40googlegroups.com.