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
+7.3k Golang : Check to see if *File is a file or directory
+11.2k Golang : How to use if, eq and print properly in html template
+16.8k Golang : Set up source IP address before making HTTP request
+4.6k Golang : How to pass data between controllers with JSON Web Token
+5k Golang : Display packages names during compilation
+6.2k Golang & Javascript : How to save cropped image to file on server
+15.7k Golang : How to login and logout with JWT example
+20.8k Golang : Convert PNG transparent background image to JPG or JPEG image
+12.1k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+9.4k Golang : Extract or copy items from map based on value
+18.6k Golang : convert int to string
+23.5k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date