如何在Go中的时区之间转换? [重复]
This question already has an answer here:
- Convert UTC to “local” time in Go 3 answers
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?
</div>
此问题已经存在 在这里有答案: p>
-
在Go中将UTC转换为“本地”时间
3个答案
span>
li>
ul>
div>
例如
time.Now() code>具有时区 p>
utcNow:= time.Now() fmt.Println(utcNow) code> pre>
输出 p>
2009-11-10 23:00:00 +0000 UTC code> pre>
我这次如何转换 到日本标准时间? p> div>
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