douyakao5308 2017-04-01 18:10
浏览 37
已采纳

Golang-时间格式奇怪的行为[重复]

This question already has an answer here:

This is a part of a bigger code. I am little confused that if I use any other digit in the below format it results in wrong values.

so instead of

fmt.Println(time.Now().Format("2006-01-02 15:04:05-07:00"))

if I try to use

 fmt.Println(time.Now().Format("2006-01-02 12:04:05-04:00"))

Result is wrong. Even when it is same format, just digit change

package main

import (
    "fmt"
    "time"
)


func main() {

    fmt.Println(time.Now().Format("2006-01-02 15:04:05-07:00"))

}

So my question is why is it so. Digits inside format have no meaning. They are just for representation of the format.

</div>
  • 写回答

1条回答 默认 最新

  • douxi2011 2017-04-01 19:08
    关注

    From https://golang.org/pkg/time/:

    func (Time) Format

    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;

    So you must use the reference time. You should not change it to another time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部