如何用Go语言获取指定时区的Hour()?

时间:2018-08-22 11:29:15

标签: go

当我使用Hour()函数时,它将不断返回UTC小时。如何获得具有指定时区的正确小时数?谢谢!

1 个答案:

答案 0 :(得分:2)

只需使用In()函数就可以了。例如:

package main

import (
    "fmt"
    "time"
)

func main() {
    loc, _ := time.LoadLocation("Europe/Berlin")

    t := time.Now().In(loc)
    fmt.Println(t.Hour())
}