timeLayout := "2006-01-02 15:04:05"
fmt.Println(time.Now().Format(timeLayout))
fmt.Println(time.Now().UTC().Format(timeLayout))
timeStr := "2021-05-18 12:00:00" // 认为服务器工夫
location, _ := time.LoadLocation("Local") // timeStr是北京工夫,留神应用Local
localTime, _ := time.ParseInLocation(timeLayout, timeStr, location)
fmt.Println(localTime.UTC().Format(timeLayout)) // 转为UTC工夫
````
阐明:留神timeStr值,此时咱们把它当做服务器工夫,所以time.LoadLocation("Local") 应用Local
### 4.UTC工夫转换为服务器工夫
阐明:留神timeStr值,此时咱们把它当做UTC工夫,所以time.LoadLocation("UTC") 应用Local
## 工夫比拟
```go
timeLayout := "2006-01-02 15:04:05"
```
```go
timeStr := "2021-05-17 11:00:00" // 认为服务器工夫
location, _ := time.LoadLocation("Local") // timeStr是北京工夫,留神应用Local
localTime, _ := time.ParseInLocation(timeLayout, timeStr, location)
fmt.Println(localTime.UTC().Format(timeLayout)) // 转为UTC工夫
compareTimeStr := "2021-05-17 02:00:00" // 认为UTC工夫
location, _ = time.LoadLocation("UTC") // timeStr是北京工夫,留神应用UTC
compareTime, _ := time.ParseInLocation(timeLayout, compareTimeStr, location)
fmt.Println(compareTime.Local().Format(timeLayout)) // 转为服务器工夫
fmt.Println("------------------------------------")
fmt.Println(compareTime.Before(localTime)) // Before 会对立工夫基准点
fmt.Println(compareTime.After(localTime)) // After 会对立工夫基准点
```