Skip to main content

How to Add Minutes to a Time in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

In Go, a length of time is a time.Duration, and the package gives you constants such as time.Minute to build one. t.Add(15 * time.Minute) returns the time 15 minutes later; later.Sub(t) gives the gap back.

Syntax​

Syntax
<later> := <t>.Add(<n> * time.Minute)
<gap> := <later>.Sub(<t>)
PartWhat it does
<n> * time.MinuteA time.Duration of <n> minutes. There are also time.Second and time.Hour.
<t>.Add(...)Returns a new time.Time; <t> itself doesn't change.
<later>.Sub(<t>)The time.Duration between the two times.

The time package documentation: "Add returns the time t+d." And for Sub: "Sub returns the duration t-u."

The Example​

The example starts at 09:30 UTC on 26 November 2026, adds 15 minutes and prints the new time and the gap.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which sets start to 09:30 UTC on 26 November 2026, sets end to start.Add(15 * time.Minute), and prints end and end.Sub(start).

main.go
package main

import (
"fmt"
"time"
)

func main() {
start := time.Date(2026, 11, 26, 9, 30, 0, 0, time.UTC)
end := start.Add(15 * time.Minute)

fmt.Println(end)
fmt.Println(end.Sub(start))
}

time.Date builds a time from its parts: year, month, day, hour, minute, second, nanosecond and location.

Step 2: Run It​

Terminal
go run main.go
Output
2026-11-26 09:45:00 +0000 UTC
15m0s

A zsh terminal where go run main.go prints 2026-11-26 09:45:00 +0000 UTC and 15m0s.

The command is the same in zsh, bash and PowerShell.

Watch the Unit​

A time.Duration counts nanoseconds. start.Add(15) compiles, but it adds 15 nanoseconds: it printed 2026-11-26 09:30:00.000000015 +0000 UTC. Always multiply by a unit such as time.Minute.

When the number of minutes is in an int variable, convert it first: time.Duration(minutes) * time.Minute gave 15m0s, while time.Minute * minutes doesn't compile.

Common Mistakes​

MistakeWhat happensDo this instead
start.Add(15)Adds 15 nanosecondsstart.Add(15 * time.Minute)
Multiplying a Duration by an int variableinvalid operation: time.Minute * m (mismatched types time.Duration and int)Convert: time.Duration(m) * time.Minute
Calling start.Add(...) without keeping the resultstart doesn't change; the new time is lostAssign the result

Troubleshooting​

SymptomLikely causeFix
A time moved by nanoseconds instead of minutesA plain number was passed to AddMultiply by time.Minute
mismatched types time.Duration and intAn int variable met a DurationWrap it in time.Duration(...)

FAQ​

How do I go back in time?​

Add a negative duration: t.Add(-15 * time.Minute).

How do I add days or months?​

t.AddDate(0, 1, 0) adds one month; AddDate takes years, months and days.

Conclusion​

t.Add(15 * time.Minute) returns the time 15 minutes later and later.Sub(t) gives the gap. Always multiply by a unit, because a bare number means nanoseconds.

Additional Resources​