time.Truncate() Function in Golang With Examples Last Updated : 21 Apr, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report In Go language, time packages supply functionality for determining as well as viewing time. The Truncate() function in Go language is used to find the outcome of rounding the stated duration ‘d’ towards zero to a multiple of ‘m’ duration. 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 (d Duration) Truncate(m Duration) Duration Here, d is the duration of time that will be rounded and m is the multiple. Return Value: It returns the outcome of rounding the stated duration 'd' towards zero to a multiple of 'm' duration. But if m is less than or equal to zero then it returns unaltered 'd'. Example 1: C // Golang program to illustrate the usage of // Truncate() function // Including main package package main // Importing fmt and time import ( "fmt" "time" ) // Calling main func main() { // Defining duration // of Truncate method tr, _ := time.ParseDuration("45m32.67s") // Prints truncated duration fmt.Printf("Truncated duration is : %s", tr.Truncate(2*time.Second)) } Output: Truncated duration is : 45m32s Here, 'd' is rounded to the multiple of m. Example 2: C // Golang program to illustrate the usage of // Truncate() function // Including main package package main // Importing fmt and time import ( "fmt" "time" ) // Calling main func main() { // Defining duration of Truncate method tr, _ := time.ParseDuration("7m11.0530776s") // Array of m t := []time.Duration{ time.Microsecond, time.Second, 4 * time.Second, 11 * time.Minute, } // Using for loop and range to // iterate over an array for _, m := range t { // Prints rounded d of all // the items in an array fmt.Printf("Truncated(%s) is : %s\n", m, tr.Truncate(m)) } } Output: Truncated(1µs) is : 7m11.053077s Truncated(1s) is : 7m11s Truncated(4s) is : 7m8s Truncated(11m0s) is : 0s Here, an array of 't' is formed first then a range is used in order to iterate over all the values of duration 't'. And at last Truncate() method is used to print all the rounded values of t in the above code. Comment More infoAdvertise with us Next Article time.Until() Function in Golang With Examples N nidhi1352singh Follow Improve Article Tags : Go Language GoLang-time Similar Reads time.Time.Truncate() Function in Golang with Examples 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 define 2 min read time.Until() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Until() function in Go language holds time value t and is used to evaluate the duration until the time t. Moreover, this function is defined under the time package. Here, you need to import the "time" p 2 min read time.Unix() Function in Golang with Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Unix() function in Go language is used to yield the local time which is related to the stated Unix time from January 1, 1970, in UTC. Moreover, this function is defined under the time package. Here, you 2 min read time.Time.UTC() Function in Golang with Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Time.UTC() function in Go language is used to yield "t" with the location that is set to UTC. Moreover, this function is defined under the time package. Here, you need to import the "time" package in or 2 min read time.Time.Unix() Function in Golang with Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Unix() function in Go language is used to yield "t" as a Unix time that is the number of seconds passed from January 1, 1970, in UTC and the output here doesn't rely upon the location connected wit 2 min read time.Time.In() Function in Golang with Examples In Go language, time packages supplies functionality for determining as well as viewing time. The In() function in Go language is used to find a copy of the stated "t" that represents the identical time instant, but along with the copy's location data that is set to "loc" for display uses. And if "l 2 min read Like