Go time.Time String() 添加“+0000 +0000”,为什么?

我正在从 Postgres 数据库中检索时间。

假设它是:

2020-02-27 08:57:36.774147+00

现在我想把它像字符串一样写在输出中,我这样做:

var myTime time.Time
fmt.Println(myTime.String())

在输出中它写了一些不同的东西:

2020-02-27 08:57:36.774147 +0000 +0000

我需要该值相同,因为我需要用它查询其他内容。


也许这里描述了同样的问题:https ://github.com/golang/go/issues/11712


长风秋雁
浏览 118回答 1
1回答

Qyouu

您需要的功能是time.Format. 将“myTime.String()”替换为“myTime.Format(layout)”,并将所需布局作为参数(例如,预定义的“time.RFC3339”或参考时间格式“2006-01-02 15:04:05.000000- 07")。去文档:func (t Time) Format(layout string) string    Format returns a textual representation of the time value formatted    according to layout, which defines the format by showing how the reference    time, defined to be        Mon Jan 2 15:04:05 -0700 MST 2006    would be displayed if it were the value; it serves as an example of the    desired output. The same display rules will then be applied to the time    value.    A fractional second is represented by adding a period and zeros to the end    of the seconds section of layout string, as in "15:04:05.000" to format a    time stamp with millisecond precision.    Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard and    convenient representations of the reference time. For more information about    the formats and the definition of the reference time, see the documentation    for ANSIC and the other constants defined by this package.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go