go - 使用Z精度格式化Go时间

标签 go

我有以下Go代码:

now := time.Now().UTC().String()
log.Info("time is: " + now)
当它运行时,将输出:
time is: 2020-08-21 10:34:43.3547088 +0000 UTC
我只想要HH:mm:ss中的时间精度,这样它将输出为:
time is: 2020-08-21 10:34:43Z
我需要更改什么才能正确格式化时间?末尾必须包含“Z”。

最佳答案

您可以使用time format获得所需的时间。


func main() {
    now := time.Now().UTC().Format("2006-01-02 15:04:05Z")
    fmt.Println("time is: " + now)
}

playground

关于go - 使用Z精度格式化Go时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63521304/

相关文章:

go - 在 Go 中声明类型时的方法歧义

unit-testing - 测试以检查功能是否未运行?

go - 在 Sublime (Golang)中重命名变量/函数的常见做法是什么?

go - Prometheus type Collector - 如何用我自己的数据提供 map

heroku - 在 Heroku 上使用第三方包进行 Go 项目

go - 当连接保持事件状态时,我得到太多打开的连接

go - 使 GO 构建适应不同的操作系统

递归链接对象的算法

php - Go有像php一样的序列化吗?

json - 如何检查 JSON 是否包含在 golang 中的数组 JSON 对象中