Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Convert time between different timezones in Go (Golang)

Posted on January 31, 2023January 31, 2023 by admin

Every time.Time object has an associated location value. When you change the location of any time.Time object to any other location, then that instant of time is not changed. Only the location value associated with that time changes. The location associated with the time.Time object only has a presentation or display logic.

The In function can be used to change the location associated with a particular time.Time object. Whenever the In function is called on any time.Time object (say t) then,

  • A copy of t is created representing the same time instant.
  • t’s location is set to the location passed to In function for display purposes
  • t is returned back

Let’s see the below-working code which can be used to change the location value associated with a particular time.

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("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: 2023-01-31 18:09:41.705858 +0000 UTC
Berlin Time: 2023-01-31 19:09:41.705858 +0100 CET
New York Time: 2023-01-31 13:09:41.705858 -0500 EST
Dubai Time: 2023-01-31 22:09:41.705858 +0400 +04
  • time package
  • time.Time
  • Recent Posts

    • Check if an item exists in a slice in Go (Golang)
    • Get the IP address from an incoming HTTP request
    • Understanding WaitGroup in Go (Golang)
    • Wait for all Go routines to finish execution in Golang
    • Detect OS in Go (Golang)
    ©2023 Welcome To Golang By Example | Design: Web XP