Golang ProgramsGolang Programs

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)
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
}
C:\golang\time>go run t1.go
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

C:\golang\time>