首页 > 解决方案 > 如何在 Go 语言中获取指定时区的 Hour()?

问题描述

当我使用 Hour() 函数时,它会一直返回 UTC 小时。如何获得指定时区的正确小时数?谢谢!

标签: go

解决方案


只需使用In()功能,它应该可以工作。例如:

package main

import (
    "fmt"
    "time"
)

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

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

推荐阅读