如何在 Golang 中获取其他时区的当前时间戳?
技术问答
284 人阅读
|
0 人回复
|
2023-09-11
|
我需要获得不同时区的当前时间。
. a: f# Y3 Q/ L1 |+ {& ~" ~目前,我知道我们可以做到以下几点:
" f& w) ~. ?6 R |; Nt := 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))
+ J* w4 s0 a2 K* W" l. D2 l5 B LoadLocation 名称被视为和 IANA 时区数据库中文件对应的位置名称,如America/New_York”。# X& ?6 o" s4 f) q% p, I
如果给出国家名称或 GMT 偏移量,如印度的 530,是否有更简单的方法来获得当前时间?
`* Y' J# {. c- ^
! @1 u6 T! b1 L4 U 解决方案: 1 k6 H4 E4 S& `$ A7 }7 B: }$ E. b. ^
init the locloc,_ := time.LoadLocation("Asia/Shanghai")//set timezone, now := time.Now().In(loc) |
|
|
|
|
|