Getting the current date and time with timestamp in local and other timezones in Golang

We can get the current date and time with a timestamp in local with the help of Location() function and other timezones in Golang with the help of LoadLocation() function. The LoadLocation() takes a string(city name) as parameter and returns the place.
.location() takes no parameter and return location .time.now() returns current date and time with a timestamp in local.

1. location(): It takes no parameter and returns local time.

2. func LoadLocation(name string) (*Location, error): It takes the place name as a parameter. Returns the location with the given name or return nil as an error.

3. time.Now() : It return current time.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import (
    "fmt"
    "time"
)
  
//main function
func main() {
  
    // with the help of time.Now()
    // store the local time
    t := time.Now()
      
    // print location and local time
    fmt.Println("Location : ", t.Location(), " Time : ", t)
}

chevron_right


Output:

Location :  Local  Time :  2009-11-10 23:00:00 +0000 UTC m=+0.000000001

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

package main
  
import (
    "fmt"
    "time"
)
  
// main function
func main() {
  
    // with the help of time.Now() 
    // store the local time
    t := time.Now()
      
    // print location and local time
    location, err := time.LoadLocation("America/New_York")
    if err != nil {
        fmt.Println(err)
    }
      
    // America/New_York
    fmt.Println("Location : ", location, " Time : ", t.In(location)) 
}

chevron_right


Output:

Location :  America/New_York  Time :  2009-11-10 18:00:00 -0500 EST



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


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