0

For example time.Now() has a timezone of UTC.

utcNow := time.Now()
fmt.Println(utcNow)

Outputs

2009-11-10 23:00:00 +0000 UTC

How do I convert this time to Japan Standard time?

Neil
  • 8,925
  • 10
  • 44
  • 49

1 Answers1

4

It looks like you're running that in the Go playground, which is why the time is automatically set to UTC (it's also always set to November 2009 when a program is launched).

If you run time.Now() on your own machine, it should pick up the local region. Alternatively, if you want to force the time to be in a specific timezone, you can use a time.Location object along with the time.Time.In function.

l, err := time.LoadLocation("Asia/Tokyo") // Look up a location by it's IANA name.
if err != nil {
    panic(err) // You can handle this gracefully.
}
fmt.Println(utcNow.In(l))

Note that it's still showing the same moment in time, but now with JST's offset.

For more information, look at the go documentation for the time package. http://golang.org/pkg/time

icza
  • 389,944
  • 63
  • 907
  • 827
Danver Braganza
  • 1,295
  • 10
  • 10