时间格式奇怪的行为

Time format weird behavior

这是更大代码的一部分。我有点困惑,如果我使用以下格式的任何其他数字,它会导致错误的值。

所以而不是

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

如果我尝试使用

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

结果错误。即使是相同的格式,只是数字变化

package main

import (
    "fmt"
    "time"
)


func main() {

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

}

所以我的问题是为什么会这样。格式内的数字没有意义。它们只是为了表示格式。

来自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;

所以你必须使用参考时间。您不应将其更改为其他时间。