问题描述
我需要获取不同时区的当前时间.
I need to get the current time in different time-zones.
目前,我知道我们可以执行以下操作:
Currently I know that we can do the following:
t := time.Now()
fmt.Println("Location:", t.Location(), ":Time:", t)
utc, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println("err: ", err.Error())
}
fmt.Println("Location:", utc, ":Time:", t.In(utc))
LoadLocation名称被视为与IANA时区数据库中的文件相对应的位置名称,例如"America/New_York".
The LoadLocation name is taken to be a location name corresponding to a file in the IANA Time Zone database, such as "America/New_York".
如果给定国家名称或GMT偏移量(例如,印度为+530),是否有更简单的方法来获取当前时间?
Is there a simpler way to get the current time, if the country name, or the GMT offset is given e.g +530 for India?
我也想支持日光节约.
推荐答案
//init the loc
loc, _ := time.LoadLocation("Asia/Shanghai")
//set timezone,
now := time.Now().In(loc)
这篇关于如何获取Golang其他时区的当前时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!