如何在 Golang 中获取其他时区的当前时间戳?
技术问答
329 人阅读
|
0 人回复
|
2023-09-11
|
我需要获得不同时区的当前时间。+ |" F! X; h; p1 @% J+ D) s& A
目前,我知道我们可以做到以下几点:' F6 Y: N6 e, J/ b" X; n
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))
! V5 D w9 c, I+ y, Z% F9 e LoadLocation 名称被视为和 IANA 时区数据库中文件对应的位置名称,如America/New_York”。. o9 A3 A4 i* d
如果给出国家名称或 GMT 偏移量,如印度的 530,是否有更简单的方法来获得当前时间?
/ `" G+ Z( x, S, y7 |5 j4 p 5 [3 ?' B* @( l) {
解决方案: " S0 p% `# n2 V8 f; o5 n
init the locloc,_ := time.LoadLocation("Asia/Shanghai")//set timezone, now := time.Now().In(loc) |
|
|
|
|
|