Skip to content
Related Articles

Related Articles

Improve Article

time.Time.UTC() Function in Golang with Examples

  • Last Updated : 19 Apr, 2020

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 order to use these functions.

Syntax:

func (t Time) UTC() Time

Here, “t” is the stated time in UTC.

Return Value: It returns t with the location that is set to UTC.

Example 1:






// Golang program to illustrate the usage of
// Time.UTC() function
  
// Including main package
package main
  
// Importing fmt and time
import (
    "fmt"
    "time"
)
  
// Calling main
func main() {
  
    // Defining t for UTC method
    t := time.Date(2020, 11, 14, 11, 30, 32, 0, time.UTC)
  
    // Calling UTC method
    utc := t.UTC()
  
    // Prints output
    fmt.Printf("%v\n", utc)
}

Output:

2020-11-15 21:23:32 +0000 UTC

Example 2:




// Golang program to illustrate the usage of
// Time.UTC() function
  
// Including main package
package main
  
// Importing fmt and time
import (
    "fmt"
    "time"
)
  
// Calling main
func main() {
  
    // Defining t for UTC method
    t := time.Date(2020, 23, 40, 56, 70, 0, 0, time.UTC)
  
    // Calling UTC method
    utc := t.UTC()
  
    // Prints output
    fmt.Printf("%v\n", utc)
}

Output:

2021-12-12 09:10:00 +0000 UTC

Here, the time “t” stated in the above code have values that are out of usual range but they are normalized while conversion.




My Personal Notes arrow_drop_up
Recommended Articles
Page :