In Go language, *time* packages supplies functionality for determining as well as viewing time. The **Time.Truncate()** function in Go language is used to find the output of rounding the stated time “t” to the closest multiple of the given duration “d” from the zero time. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.

**Syntax:**

func (t Time) Truncate(d Duration) Time

Here, “t” is the stated time, and “d” is the given duration.

**Note:** The Truncate() method works on the time in the form of an absolute duration from zero time. However, it doesn’t work on the layout form of the time.

**Return Value:** It returns the output of rounding the given time “t” to the closest multiple of the stated duration “d”. Where, if d is less than or equal to zero then it returns “t” out of any monotonic clock reading but else unaltered.

**Example 1:**

`// Golang program to illustrate the usage of ` `// Time.Truncate() function ` ` ` `// Including main package ` `package main ` ` ` `// Importing fmt and time ` `import ` `"fmt"` `import ` `"time"` ` ` `// Calling main ` `func main() { ` ` ` ` ` `// Defining t for Truncate method ` ` ` `t := ` `time` `.Date(2007, 7, 6, 23, 58, 11, 60, ` `time` `.UTC) ` ` ` ` ` `// Defining duration ` ` ` `d := (60 * ` `time` `.Second) ` ` ` ` ` `// Calling Truncate() method ` ` ` `trunc := t.Truncate(d) ` ` ` ` ` `// Prints output ` ` ` `fmt.Printf(` `"The result after rounding 't' is: %v\n"` `, trunc) ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The result after rounding 't' is: 2007-07-06 23:58:00 +0000 UTC

**Example 2:**

`// Golang program to illustrate the usage of ` `// Time.Truncate() function ` ` ` `// Including main package ` `package main ` ` ` `// Importing fmt and time ` `import ` `"fmt"` `import ` `"time"` ` ` `// Calling main ` `func main() { ` ` ` ` ` `// Defining t for Truncate method ` ` ` `t := ` `time` `.Date(2047, 47, 96, 123, 98, 81, 999434, ` `time` `.UTC) ` ` ` ` ` `// Defining duration ` ` ` `d := (2 * ` `time` `.Hour) ` ` ` ` ` `// Calling Truncate() method ` ` ` `trunc := t.Truncate(d) ` ` ` ` ` `// Prints output ` ` ` `fmt.Printf(` `"The result after rounding 't' is: %v\n"` `, trunc) ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The result after rounding 't' is: 2051-02-09 04:00:00 +0000 UTC

Here, the “t” stated in the above code has values that are outside the usual range but they are normalized while conversion.

## Recommended Posts:

- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- atomic.AddInt64() Function in Golang With Examples
- atomic.StoreInt64() Function in Golang With Examples
- reflect.FieldByIndex() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- bits.Sub() Function in Golang with Examples
- io.PipeWriter.CloseWithError() Function in Golang with Examples
- time.Round() Function in Golang With Examples
- reflect.AppendSlice() Function in Golang with Examples
- reflect.ChanOf() Function in Golang with Examples
- flag.Bool() Function in Golang With Examples
- time.Sleep() Function in Golang With Examples
- time.Time.Year() Function in Golang with Examples
- reflect.DeepEqual() Function in Golang with Examples
- reflect.Indirect() Function in Golang with Examples
- reflect.CanAddr() Function in Golang with Examples
- reflect.CanInterface() Function in Golang with Examples
- reflect.CanSet() Function in Golang with Examples
- reflect.Cap() Function in Golang with Examples

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.