Subtract time.Duration from time in GO [SOLVED]


Written By - Tuan Nguyen

In Golang, we usually have to deal with time management, we do need to do some operations such as adding, and subtracting a duration from time. We will walk through some examples of Subtracting time.Duration from time in Go. The built-in package time provides functionality for measuring and displaying time. The calendrical calculations always assume a Gregorian calendar, with no leap seconds.

 

Example 1: time.Add() function in Golang

func (t Time) Add(d Duration) Time: Add returns the time t+d.

In Golang, if you want to add a duration to a time, just use the time.Add() function. Here's an example of how to add a duration to a time value in Go to (hours, minutes, and seconds):

package main

import (
	"fmt"
	"time"
)

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

	// Add 2 hour to now
	fmt.Println(now.Add(time.Hour * 1))

	// Add 30 minutes to now
	fmt.Println(now.Add(time.Minute * 30))

	// Add 10 seconds to now
	fmt.Println(now.Add(time.Second * 10))
}

Output:

2022-12-31 20:44:50.0359545 +0700 +07 m=+0.001597801
2022-12-31 21:44:50.0359545 +0700 +07 m=+3600.001597801
2022-12-31 21:14:50.0359545 +0700 +07 m=+1800.001597801
2022-12-31 20:45:00.0359545 +0700 +07 m=+10.001597801

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

 

Example 2: Subtract time.Duration from time.Time

We also can do the subtraction time.Duration from time.Time by using the Add function. Noted that we will use the minus sign to do the subtraction. The code below shows how to subtract hours, minutes, and seconds from a time.Time in Golang:

package main

import (
	"fmt"
	"time"
)

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

	// Subtract 2 hour from now
	fmt.Println(now.Add(-time.Hour * 1))

	// Subtract 30 mins from now
	fmt.Println(now.Add(-time.Minute * 30))

	// Subtract 30 seconds from now
	fmt.Println(now.Add(-time.Second * 30))
}

Output:

2022-12-31 20:51:33.4318321 +0700 +07 m=+0.001589101
2022-12-31 19:51:33.4318321 +0700 +07 m=-3599.998410899
2022-12-31 20:21:33.4318321 +0700 +07 m=-1799.998410899
2022-12-31 20:51:03.4318321 +0700 +07 m=-29.998410899

 

Example 3: Subtract years, months, and days from time.Time

In the below example AddDate() is used from the time package. Noted that if we want to do the subtraction, we need to use the negative numbers before the duration as shown below:

func (t Time) AddDate(years int, months int, days int) Time: AddDate returns the time corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	fmt.Println(now)
	// Subtract one year from now
	fmt.Println(now.AddDate(-1, 0, 0))

	// Subtract one month from now
	fmt.Println(now.AddDate(0, -1, 0))

	// Subtract one day from now
	fmt.Println(now.AddDate(0, 0, -1))
}

Output:

2022-12-31 20:55:40.6590009 +0700 +07 m=+0.001594301
2021-12-31 20:55:40.6590009 +0700 +07
2022-12-01 20:55:40.6590009 +0700 +07
2022-12-30 20:55:40.6590009 +0700 +07

 

Example 4: The time.Sub() function in Golang

You may wonder if we can use the Add() function to add a duration to a time.Time, why don't we use the Sub() function to subtract a duration from a time? From the official document, the Sub() function returns the duration t-u. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. In this example, we will try to use this function to calculate the duration between 2 time.Time:

package main

import (
	"fmt"
	"time"
)

func main() {
	// create a new time.Time
	start := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
	end := time.Now()

	diff := end.Sub(start)
	fmt.Printf("difference = %v\n", diff)
}

Output:

difference = 8749h59m34.6985773s

 

Summary

In this article, we already walk through some examples of adding and subtraction a duration to a time.Time in Golang. For subtraction of hours, minutes, seconds,.. we can use the Add() function with the minus symbol before the duration. When we want to subtract years, months, and days from a time, we can use the AddDate() function. Golang provides us with the Sub() function but it is not used to subtract a duration from a time. It is used when you want to find the difference between 2 time.Time.

 

References

https://pkg.go.dev/time#Time.AddDate

 

Categories GO

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment