Golang : Get local time and equivalent time in different time zone
Problem :
You want to show your website visitor the local time(on your server) and the equivalent time in another time zone. For example, KL(server time) and New York time.
Solution :
- Get local server time.
- Convert the local time to target timezone with
time.In()
function.
For example :
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println("Location : ", t.Location(), " Time : ", t)
// get the list of available time zones
location, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println(err)
}
fmt.Println("Location : ", location, " Time : ", t.In(location))
}
Sample output :
Location : Local Time : 2015-07-30 16:39:28.158021072 +0800 MYT
Location : America/New_York Time : 2015-07-30 04:39:28.158021072 -0400 EDT
References :
See also : Golang : How to get time zone and load different time zone?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.6k Golang : Replace a parameter's value inside a configuration file example
+10k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+15.4k CodeIgniter/PHP : Create directory if does not exist example
+18.3k Golang : Count JSON objects and convert to slice/array
+5.2k Golang : Join lines with certain suffix symbol example
+7.3k Golang : How to check variable or object type during runtime?
+3.9k MariaDB/MySQL : Form select statement or search query with Chinese characters
+8.8k Golang : Test a slice of integers for odd and even numbers
+50.4k Golang : How to get struct field and value by name
+11.3k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+6.9k Golang : Get all countries phone codes
+9.5k Golang : Fix go.exe is not compatible with the version of Windows you're running