How to Add or Subtract Time in Go

This article offers concise instructions for manipulating time in Go, including adding and subtracting time units, calculating time differences, and adjusting dates, essential for developers working with time data.

Let’s get started!

Adding durations to a Time value

In Go, you can easily add time intervals to a time.Time value. This includes hours, minutes, and seconds. Here’s an example:

go
package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	fmt.Println(now)

	// Add one hour to current time
	fmt.Println(now.Add(time.Hour * 1))
	// Add 30 minutes to current time
	fmt.Println(now.Add(time.Minute * 30))
	// Add 10 seconds to current time
	fmt.Println(now.Add(time.Second * 10))
}
output
2024-01-26 20:47:44.448272962 +0100 WAT m=+0.000012013
2024-01-26 21:47:44.448272962 +0100 WAT m=+3600.000012013
2024-01-26 21:17:44.448272962 +0100 WAT m=+1800.000012013
2024-01-26 20:47:54.448272962 +0100 WAT m=+10.000012013

You can also add smaller units like milliseconds, microseconds, and nanoseconds in the same manner.

Subtracting durations from a Time value

Subtraction works similarly, by adding negative durations:

go
package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	fmt.Println(currentTime)
	// Subtract one hour from current time
	fmt.Println(currentTime.Add(-time.Hour * 1))
	// Subtract 30 minutes from currentTime
	fmt.Println(currentTime.Add(-time.Minute * 30))
	// Subtract 10 seconds from currentTime
	fmt.Println(currentTime.Add(-time.Second * 10))
}
output
2024-01-26 20:49:28.723475889 +0100 WAT m=+0.000011469
2024-01-26 19:49:28.723475889 +0100 WAT m=-3599.999988531
2024-01-26 20:19:28.723475889 +0100 WAT m=-1799.999988531
2024-01-26 20:49:18.723475889 +0100 WAT m=-9.999988531

Calculating the difference between two Time values

To find the duration between two time.Time values, use time.Time.Sub():

go
package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	startOfYear := time.Date(time.Now().Year(), 01, 01, 0, 0, 0, 0, time.UTC)

	result := now.Sub(startOfYear)

	fmt.Println(result)
}
output
619h55m49.326123021s

Adding years, months, and days

The time.Time.AddDate() method allows you to add years, months, and days to a time.Time value:

go
package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	fmt.Println(now)

	fmt.Println(now.AddDate(0, 0, 1)) // Add one day to current time

	fmt.Println(now.AddDate(0, 1, 0)) // Add one month to current time

	fmt.Println(now.AddDate(1, 0, 0)) // Add one year to current time
}
output
2024-01-26 20:59:36.483427666 +0100 WAT m=+0.000012233
2024-01-27 20:59:36.483427666 +0100 WAT
2024-02-26 20:59:36.483427666 +0100 WAT
2025-01-26 20:59:36.483427666 +0100 WAT

Subtracting years, months, and days

Subtracting years, months, and days is similar, just use negative values:

go
package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()

	fmt.Println(now)

	fmt.Println(now.AddDate(-1, -1, 1)) // Subtract one year and one month
}
output
2024-01-26 21:02:24.780663912 +0100 WAT m=+0.000010559
2022-12-27 21:02:24.780663912 +0100 WAT

Thanks for reading, and happy coding!