down00112 2017-09-12 03:22
浏览 152

了解时间包的golang日期格式

So I have the function performing well.

func Today()(result string){
    current_time := time.Now().Local()
    result =  current_time.Format("01/02/2006")
    return
}

Prints MM/DD/YYYY And I thought that it would be more readable if I had a value greater than 12 in the days position to make it clear that it was MM/DD/YYYY so I changed the to following

func Today()(result string){
    current_time := time.Now().Local()
    result =  current_time.Format("01/23/2004")
    return
}

Which to my chagrin caused bad results. Prints MM/DDHH/DD0MM

Realizing my mistake I see that the format is defined by the reference time...

Mon Jan 2 15:04:05 -0700 MST 2006

I'm wondering if there is any other instances this moment being used as a formatting reference for date times, and if this reference moment has a nickname (like null island)?

  • 写回答

1条回答 默认 最新

  • duandi8852752 2017-09-12 07:04
    关注

    The values in a date string are not arbitrary. You can't just change 02 to 03 and expect it to work. The date formatter looks for those specific values, and knows that 1 means month, 2 means day of month, etc.

    Changing 01/02/2006 to 01/23/2004 is like changing a human-readable form that says First Name: ______ Last Name: ______ to one that says First Name: ______ Ice Cream: ______. You can't expect anyone to know that Ice Cream should mean Last Name.

    The name

    The only name provided for this is "reference time", here:

    Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be

    Mon Jan 2 15:04:05 -0700 MST 2006
    

    and here:

    These are predefined layouts for use in Time.Format and Time.Parse. The reference time used in the layouts is the specific time:

    Mon Jan 2 15:04:05 MST 2006
    

    which is Unix time 1136239445. Since MST is GMT-0700, the reference time can be thought of as

    01/02 03:04:05PM '06 -0700
    

    To define your own format, write down what the reference time would look like formatted your way; see the values of constants like ANSIC, StampMicro or Kitchen for examples. The model is to demonstrate what the reference time looks like so that the Format and Parse methods can apply the same transformation to a general time value.

    To specify that you're talking about Go's reference time, I'd say "Go's reference time." Or to be blatantly obvious, "Go's time.Parse reference time."


    As an aside, your function can be greatly shortened:

    func Today() string {
        return time.Now().Local().Format("01/02/2006")
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b