Go 时区设置
- 获取 time 对象
- 设置时区
按设置的时区输出
package main import ( "fmt" "time" ) func main() { now := time.Now() // 1. 获取 time 对象 // 2. 设置时区 local1, err1 := time.LoadLocation("") //等同于"UTC" if err1 != nil { fmt.Println(err1) } local2, err2 := time.LoadLocation("Local")//本地的时区 if err2 != nil { fmt.Println(err2) } local3, err3 := time.LoadLocation("America/Los_Angeles") if err3 != nil { fmt.Println(err3) } // 3. 按设置的时区输出 fmt.Println(now.In(local1)) fmt.Println(now.In(local2)) fmt.Println(now.In(local3)) } // out: // 2019-09-11 08:15:38.8719044 +0000 UTC // 2019-09-11 16:15:38.8719044 +0800 CST // 2019-09-11 01:15:38.8719044 -0700 PDT
- 原文作者:TomatoAres
- 原文链接:https://TomatoAres.github.io/posts/go-%E6%97%B6%E5%8C%BA%E8%AE%BE%E7%BD%AE/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。