go语言UTC时间转换为中国时间

go语言UTC时间转换为中国时间

在Go语言中,处理时间时默认使用的是UTC(协调世界时)

go 复制代码
package main  
  
import (  
	"fmt"  
	"time"  
)  
  
func main() {  
	// 获取当前UTC时间  
	now := time.Now().UTC()  
	fmt.Println("UTC Time:", now)  
  
	// 获取中国时区(东八区)  
	location, err := time.LoadLocation("Asia/Shanghai")  
	if err != nil {  
		fmt.Println("Error loading location:", err)  
		return  
	}  
  
	// 将UTC时间转换为中国时间  
	chinaTime := now.In(location)  
	fmt.Println("China Time:", chinaTime)  
}

time.LoadLocation("Asia/Shanghai")用于加载中国上海的时区信息,它代表了中国的标准时间(即北京时间,UTC+8)。

now.In(location)方法则是将UTC时间now转换为指定时区location的时间。

相关推荐
IT_陈寒40 分钟前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
用户395240998802 小时前
排坑日记:ASP.NET Core 中 "Required field is not provided" 验证错误全记录
后端
用户8356290780512 小时前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
Oneslide3 小时前
sudo免密权限配置不生效
后端
站大爷IP3 小时前
为什么Python不用var或let声明变量?
后端
赴星半途3 小时前
NestJS实战-创建AuthService
后端
北冥有鱼3 小时前
mqtt 测试
前端·后端
代码丰3 小时前
使用 TtlExecutors 解决线程池中的 ThreadLocal 上下文丢失问题
后端
阿祖zu4 小时前
别再优化 RAG 了,适配 Agent 的 LLM Wiki 知识库理念
前端·后端·aigc