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
+6.2k Golang : Calculate diameter, circumference, area, sphere surface and volume
+10k Golang : Convert file unix timestamp to UTC time example
+11.8k Golang : Get remaining text such as id or filename after last segment in URL path
+7.7k Golang : What fmt.Println() can do and println() cannot do
+13.2k Golang : How to get year, month and day?
+6.4k Golang : Derive cryptographic key from passwords with Argon2
+5.9k Golang : Process non-XML/JSON formatted ASCII text file example
+21.7k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+7.5k Golang : Reverse a string with unicode
+9.9k Golang : Print how to use flag for your application example
+5.5k Unix/Linux : How to test user agents blocked successfully ?
+19.2k Golang : How to Set or Add Header http.ResponseWriter?