Golang中将UTC日期格式转换为所需格式的方法
Golang中将UTC日期格式转换为所需格式的方法
需要转换存储在数据库中的UTC时区时间。类似这样 -
2020-02-21 10:29:42.812149 +0000 UTC
而我期望得到的是 -
Fri, 21 Feb 2020 13:29:42 +03
我有一个函数,用于返回转换后的日期版本 -
var location, _ = time.LoadLocation("Europe/Minsk")
func convertToResponse(books Book) BookResponse {
return BookResponse{
ID: books.ID,
Name: books.Name,
Author: books.Author,
Date: books.Date.In(location).Format(time.RFC1123),
}
}
我创建了一个从数据库中选择数据的端点,但当我运行它时,日期格式却是这样的 -
{
"id": 19,
"name": "book19",
"author": "QWERT",
"date": "Sat, 01 Jan 0000 12:17:16 LMT"
}
它没有被正确格式化。我不确定哪里出了问题。 这是完整代码的链接,如果你需要的话 -
JustinObanor/lets_Go
一系列用Go编写的任务。通过在GitHub上创建账户,为JustinObanor/lets_Go的开发做出贡献。
更多关于Golang中将UTC日期格式转换为所需格式的方法的实战教程也可以访问 https://www.itying.com/category-94-b0.html
books.Date 的确切值是什么?books.Date.In(location) 的值又是什么?
你可以从官方的 time 包或以下内容获取帮助:
![]()
Go 日期和时间格式化 - GolangLearn

在本教程中,我将向你介绍使用 Go 语言 time 包处理日期时间的一些基本格式。Go 提供了 time 包,其中包含一些便捷的功能。
books.Date 的值是 -
0000-01-01 10:27:00.138572 +0000 UTC
这只是UTC时区的日期。原始格式。
Need to convert the UTC time zones I stored in my database. Something like this -
2020-02-21 10:29:42.812149 +0000 UTC
需要转换我存储在数据库中的UTC时区。类似这样 -
2020-02-21 10:29:42.812149 +0000 UTC
books.Date.In(location) 的值应该是将UTC时区转换为我期望的所需位置的格式。
and what I expect to get us -
Fri, 21 Feb 2020 13:29:42 +03
而我期望得到的是 -
Fri, 21 Feb 2020 13:29:42 +03
也许这个资源会有所帮助
在 Go 中将 UTC 转换为“本地”时间
标签: time, go, timezone, timezone-offset
由 peterSO 于 11:52PM - 14 Aug 14 UTC 回答


