How to get the current date and time with timestamp in local and other timezones ?
LoadLocation returns the Location with the given name.
Syntax
func LoadLocation(name string) (*Location, error)
Example
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println("Location : ", t.Location(), " Time : ", t) // local time
location, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println(err)
}
fmt.Println("Location : ", location, " Time : ", t.In(location)) // America/New_York
loc, _ := time.LoadLocation("Asia/Shanghai")
now := time.Now().In(loc)
fmt.Println("Location : ", loc, " Time : ", now) // Asia/Shanghai
}
Output
Location : Local Time : 2017-08-26 21:04:56.1874497 +0530 IST
Location : America/New_York Time : 2017-08-26 11:34:56.1874497 -0400 EDT
Location : Asia/Shanghai Time : 2017-08-26 23:34:56.1884498 +0800 CST
Most Helpful This Week
How to import structs from another package in Go?
How to concatenate two or more slices in Golang?
Get Year, Month, Day, Hour, Min and Second from a specified date
Golang Get current Date and Time in EST, UTC and MST?
Functions mess (recursive anonymous function) in Golang
Pass different types of arguments in variadic function