- 获取 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