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

In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Location() function in Go language is used to check the data of the time zone that is associated with “t”. 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) Location() *Location

Here, “t” is the stated time and *Location is the pointer to Location.

Return value: It returns the information of the time zone that is associated with “t”.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// Time.Location() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining t for calling Location method
    t := time.Date(2019, 2, 11, 10, 03, 00, 00, time.UTC)
  
    // Calling Location method
    loc := t.Location()
  
    // Prints output
    fmt.Printf("%v\n", loc)
}

chevron_right


Output:

UTC

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// Time.Location() 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 Location method
    t := time.Date(2019, 2, 11, 10, 03, 00, 00, location)
  
    // Calling Location method
    loc := t.Location()
  
    // Prints output
    fmt.Printf("%v\n", loc)
}

chevron_right


Output:

UTC-7

Here, FixedZone() method is used in order to define the location parameter of Date() method so the data of the time zone in the output is returned according to that location.




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.