为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
长风秋雁 2022-05-18 16:06:32
我正在从 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
查看完整描述

1 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

您需要的功能是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.


查看完整回答
反对 回复 2022-05-18
  • 1 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信