time.Time.Local() Function in Golang with Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Local() function in Go language is used to find “t” with the location that is set to local 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) Local() Time
Here, “t” is the stated time.
Return Value: It returns “t” along with the location that is set to local time.
Example 1:
// Golang program to illustrate the usage of // Time.Local() function // Including main package package main // Importing fmt and time import "fmt" import "time" // Calling main func main() { // Defining t parameter of Local method t := time .Date(2019, 2, 11, 10, 03, 00, 00, time .UTC) // Calling Local method local := t.Local() // Prints output fmt.Printf( "%v\n" , local) } |
Output:
2019-02-11 10:03:00 +0000 UTC
Example 2:
// Golang program to illustrate the usage of // Time.Local() function // Including main package package main // Importing fmt and time import "fmt" import "time" // Calling main func main() { // Defining location using FixedZone method location := time .FixedZone( "UTC-7" , -6*56*34) // Defining t for calling Local method t := time .Date(2019, 2, 11, 10, 03, 00, 00, location) // Calling Local method local := t.Local() // Prints output fmt.Printf( "%v\n" , local) } |
Output:
2019-02-11 13:13:24 +0000 UTC
Here, FixedZone() method is used in order to define the location parameter of the Date() method so the time in output is returned according to that location.
Contact Us