How to Convert Between Time Zones in Go

Convert Go time.Time values between time zones using time.LoadLocation and the In method.

Use the time.LoadLocation function to get a *time.Location for your target zone, then call In on your time.Time value to convert it.

package main

import (
	"fmt"
	"time"
)

func main() {
	// Create a time in UTC
	t := time.Now().UTC()

	// Load the target location (e.g., America/New_York)
	loc, err := time.LoadLocation("America/New_York")
	if err != nil {
		panic(err)
	}

	// Convert the time to the new location
	converted := t.In(loc)

	fmt.Println("UTC:", t)
	fmt.Println("New York:", converted)
}