Welcome To Golang By Example

Get current time and date of different timezones in Go (Golang)

time package has a LoadLocation(name string) function which can be used to load a location of anywhere. Once you have the location, then In function can be used to change the location associated with a particular time.Time object.

For LoadLocation(locationName) function

Let’s see an example:

package main

import (
	"fmt"
	"time"
)

func main() {
	now := time.Now()
	loc, _ := time.LoadLocation("UTC")
	fmt.Printf("UTC Time: %s\n", now.In(loc))

	loc, _ = time.LoadLocation("Local")
	fmt.Printf("Local Time: %s\n", now.In(loc))

	loc, _ = time.LoadLocation("Europe/Berlin")
	fmt.Printf("Berlin Time: %s\n", now.In(loc))

	loc, _ = time.LoadLocation("America/New_York")
	fmt.Printf("New York Time: %s\n", now.In(loc))

	loc, _ = time.LoadLocation("Asia/Dubai")
	fmt.Printf("Dubai Time: %s\n", now.In(loc))

}

Output

UTC Time: 2020-01-31 17:36:26.481946 +0000 UTC
Local Time: 2020-02-01 01:36:26.481946 +0800 +08
Berlin Time: 2020-01-31 18:36:26.481946 +0100 CET
New York Time: 2020-01-31 12:36:26.481946 -0500 EST
Dubai Time: 2020-01-31 21:36:26.481946 +0400 +04