go - 时间格式化程序的输出不正确

标签 go

<分区>

我正在尝试使用时间格式函数将时间格式化为 MMM dd yyyy, HH:mm:ss 格式,但输出的日期不正确。下面是引用代码

package main

import (
    "fmt"
    "time"
)

func main() {
    var t time.Time
    t = time.Now()
    fmt.Println(t.Format("Jan 01 2006, 15:04:05"))
    //Output
    //Dec 12 2018, 16:27:34
}

但是如果我将引用格式更改为 Jan 02 2006, 15:04:05 我会得到正确的输出。所以我无法找到两个日期引用之间的问题是什么,以及适用于所有用例的正确引用日期格式是什么。

最佳答案

Layouts must use the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to format/parse a given time/string.

来源:https://gobyexample.com/time-formatting-parsing

如果你真的想完全理解这一点,请深入研究 source .

关于go - 时间格式化程序的输出不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53984528/

相关文章:

arrays - Golang : Parsing two array of arrays in JSON, 使用相同的结构

api - 在 Go 中使用 Ticker 轮询 API

serialization - 如何反序列化非标准大小的字段?

go - 如何使用Go从gmail获取未读电子邮件?

string - 将字节 slice 转换为字符串时,Golang 会进行任何转换吗?

azure - K8S - 获取配置 map 数据问题

go - 无法加载集群内配置,必须定义 KUBERNETES_SERVICE_HOST 和 KUBERNETES_SERVICE_PORT

Go template.ExecuteTemplate 包含 html

string - 如何使用 unsafe 从没有内存复制的字符串中获取字节 slice

go - 无法将 traefik 与动态配置文件一起使用