Golang multiply duration by integer: int to time.Duration

Golang multiply time duration and fix mismatched types int and time.Duration; golang int to time.Duration with time.Duration(n)*time.Second; constants; golang time duration example; max int64 nanoseconds overflow.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang multiply duration by integer: int to time.Duration

Questions about golang multiply duration, golang multiply time duration, golang int to duration, golang convert int to time.Duration, or errors like mismatched types int and time.Duration all come down to one rule: time.Duration is its own named type (an int64 count of nanoseconds), not a plain int. Multiplication uses the same type rules as the rest of Go: typed int variables do not mix with Duration without a conversion, while untyped integer constants can adopt Duration in the right context. For constants in general, see arrays and constants in Go.

Tested with Go 1.24 on Linux.


mismatched types int and time.Duration

time.Duration is defined as nanoseconds. Constants like time.Second already have type time.Duration. If one side of * is a typed int, the compiler stops:

go
package main

import (
	"fmt"
	"time"
)

func main() {
	var timeout = 3
	fmt.Println(timeout * time.Second)
}
text
./main.go:10:14: invalid operation: timeout * time.Second (mismatched types int and time.Duration)

That message is what people paste when they search golang mismatched types int and time.Duration or int to duration golang after a failed multiply.


golang multiply time duration with constants or untyped literals

Untyped constants (including const timeout = 3) can participate in duration math because the compiler still has flexibility about their type:

go
const timeout = 3

func sleep() {
	time.Sleep(timeout * time.Second)
}

Untyped integer literals next to a Duration work the same way:

go
time.Sleep(3 * time.Second)

Both are common golang time duration example patterns.


golang int to time.Duration (typed variables)

Once you have a typed int (or int32, int64, …), convert before multiplying:

go
timeout := 3
time.Sleep(time.Duration(timeout) * time.Second)

You can also write time.Second * time.Duration(timeout); multiplication commutes.

If your integer already means nanoseconds, use time.Duration(n) alone. If it means milliseconds:

go
ms := 500
d := time.Duration(ms) * time.Millisecond

Pick the unit that matches what the integer represents—golang convert int to time.Duration is really “interpret this int in some unit, then scale by that unit’s Duration constant.”


Division and rounding

time.Duration values divide by integers with / when the divisor is an integer type compatible with the operation, or divide two Durations to get a ratio. For human rounding, see time.Round, Truncate.


go time.duration max value overflow int64 nanoseconds

Duration stores nanoseconds as int64, so the maximum is roughly 9.2e18 ns (about 290 years). Multiplying two large numbers as time.Duration(a) * time.Duration(b) is rarely what you want and can overflow silently. Typical safe pattern: convert the count only, then multiply by one unit constant:

go
seconds := int64(3600)
d := time.Duration(seconds) * time.Second

If seconds * int64(time.Second) would exceed math.MaxInt64, the result wraps—validate inputs for long-running timers.


Summary

Golang multiply duration and golang multiply time duration boil down to using either untyped constants/literals or explicit time.Duration(...) casts so both sides of * agree. The compiler error mismatched types int and time.Duration means you used a typed integer without converting. For golang int to time.Duration, multiply time.Duration(n) by time.Second, time.Millisecond, or another unit constant that matches your data. Remember int64 nanoseconds limits and overflow when values get huge.


References


Frequently Asked Questions

1. Why do I get invalid operation mismatched types int and time.Duration?

Binary * requires the same type when one operand is a typed int variable. Convert with time.Duration(n) or use an untyped constant, or write time.Second * n if n is untyped.

2. How do I convert golang int to time.Duration for seconds?

Use time.Duration(seconds) * time.Second for whole seconds, or time.Duration(ms) * time.Millisecond. Pick the unit that matches your integer meaning.

3. What is the maximum time.Duration and can it overflow?

time.Duration is int64 nanoseconds (about 290 years). Multiplying large ints can overflow silently in nanoseconds; check ranges or use big.Rat for extreme cases.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …