了解时间包的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)?

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")
}