如何获取Golang其他时区的当前时间戳? [英] How to get the current timestamp in other timezones in Golang?

查看:67
本文介绍了如何获取Golang其他时区的当前时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取不同时区的当前时间.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆