Golang : How to get time zone and load different time zone?
Problem :
You need to get current time zone and load different time zone. How to do that in Golang?
Solution :
Use the Time package. It has the Zone()
and LoadLocation()
functions. For example :
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Now: ", now)
fmt.Println(now.Format("Mon, Jan 2, 2006 at 3:04pm"))
fmt.Println("Location: ", now.Location())
// get the time zone name
z, _ := now.Zone()
fmt.Println("Location(Time Zone): ", z)
// load different time zone
est, _ := time.LoadLocation("EST")
fmt.Println("Load Location : ", est)
dayInEST := time.Date(2015, 18, 5, 12, 15, 0, 0, est)
fmt.Println("This code is created on : ", dayInEST.Format("Monday"))
}
Sample output :
Now: 2015-05-18 10:35:32.024585532 +0800 SGT
Mon, May 18, 2015 at 10:35am
Location: Local
Location(Time Zone): SGT
Load Location : EST
This code is created on : Sunday
See also : Golang : Get current, epoch time and display by year, month and day
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
+11.9k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+5.3k Golang : Generate Interleaved 2 inch by 5 inch barcode
+25.9k Golang : How to read integer value from standard input ?
+13.9k Golang : How to check if a file is hidden?
+5k Golang : Constant and variable names in native language
+48.5k Golang : Upload file from web browser to server
+34.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+5.7k Unix/Linux/MacOSx : Get local IP address
+8.3k Golang : Check if integer is power of four example
+11.8k Golang : GTK Input dialog box examples
+9.5k Golang : Get all countries currencies code in JSON format
+12.1k Golang : Split strings into command line arguments