How to add or subtract time in Go

It is often necessary to add or subtract a specific duration (such as hours, minutes or seconds) to a time value. This article will describe all the ways this task can be achieved in Go.

Add time.Duration to time.Time

Here’s how to add hours, minutes and seconds to a time value in Go:

package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	fmt.Println(currentTime)
	// Add one hour to current time
	fmt.Println(currentTime.Add(time.Hour * 1))
	// Add 30 minutes to currentTime
	fmt.Println(currentTime.Add(time.Minute * 30))
	// Add 10 seconds to currentTime
	fmt.Println(currentTime.Add(time.Second * 10))
}
Output
2021-05-26 09:06:19.2397244 +0100 WAT m=+0.000055501
2021-05-26 10:06:19.2397244 +0100 WAT m=+3600.000055501
2021-05-26 09:36:19.2397244 +0100 WAT m=+1800.000055501
2021-05-26 09:06:29.2397244 +0100 WAT m=+10.000055501

You can also add milliseconds, microseconds and nanoseconds by multiplying time.Millisecond , time.Microsecond and time.Nanosecond with the desired value.

Subtract time.Duration from time.Time

Subtracting time is also done through the Add method, except that the time unit is prefixed with a minus symbol as shown below:

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
2021-05-26 09:14:46.2119692 +0100 WAT m=+0.000053801
2021-05-26 08:14:46.2119692 +0100 WAT m=-3599.999946199
2021-05-26 08:44:46.2119692 +0100 WAT m=-1799.999946199
2021-05-26 09:14:36.2119692 +0100 WAT m=-9.999946199

Subtract one time.Time value from another one

If you want to subtract one time.Time value from another one, the method to use is time.Time.Sub(). This will produce a time.Duration value as the result. Here’s an example that utilises Sub() to produce the difference between the current time and the time at the start of the year.

package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	startOfYear := time.Date(2021, 01, 01, 0, 0, 0, 0, time.UTC)
	result := currentTime.Sub(startOfYear)
	fmt.Println(result) // 3488h22m10.4962529s
}

Add years, months and days to time.Time

It’s also possible to add years, months or days to a time value in Go through the time.Time.AddDate() method:

package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	fmt.Println(currentTime)
	// Add one day to current time
	fmt.Println(currentTime.AddDate(0, 0, 1))
	// Add one month to current time
	fmt.Println(currentTime.AddDate(0, 1, 0))
	// Add one year to current time
	fmt.Println(currentTime.AddDate(1, 0, 0))
}
Output
2021-05-26 09:37:28.6701016 +0100 WAT m=+0.000097101
2021-05-27 09:37:28.6701016 +0100 WAT
2021-06-26 09:37:28.6701016 +0100 WAT
2022-05-26 09:37:28.6701016 +0100 WAT

Subtract years, months and days from time.Time

To subtract years, months and days from a time.Time value, the AddDate() method is also used, except that the respective argument is prefixed with a minus sign:

package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	fmt.Println(currentTime)
	// Subtract one year and one month from the current time
	fmt.Println(currentTime.AddDate(-1, -1, 0))
}
Output
2021-05-26 09:42:24.5619571 +0100 WAT m=+0.000064901
2020-04-26 09:42:24.5619571 +0100 WAT

Thanks for reading, and happy coding!