Golang time.Location type and LoadLocation() function example
package time
Golang time.Location type and LoadLocation() function usage 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, err := time.LoadLocation("EST") //<---------------------- here !
if err != nil {
fmt.Println(err)
}
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"))
}
Output :
Now: 2009-11-10 23:00:00 +0000 UTC
Tue, Nov 10, 2009 at 11:00pm
Location: UTC
Location(Time Zone): UTC
Load Location : EST
This code is created on : Sunday
References :
Advertisement
Something interesting
Tutorials
+5.8k Golang : Struct field tags and what is their purpose?
+19.5k Golang : Display list of time zones with GMT
+7.6k Golang : Gorrila set route name and get the current route name
+36.6k Golang : How to split or chunking a file to smaller pieces?
+7.8k Gogland : Where to put source code files in package directory for rookie
+10.6k Swift : Convert (cast) String to Integer
+5.4k Javascript : Change page title to get viewer attention
+10.4k Golang : Convert file content to Hex
+15.3k Golang : package is not in GOROOT during compilation
+11.8k Golang : Find age or leap age from date of birth example
+12.5k Golang : How to display image file or expose CSS, JS files from localhost?