import ( "fmt" "time" ) func main() { fmt.Println(time.Now().Format("2006-01-02 15:04:05")) }
2006-01-02 15:04:05
fmt.Println(time.Now().Format("2020-01-02 15:04:05"))
和前面的例子唯一不同的是换了年份,但是这次的输出是:
❯ go run simple.go 2020-07-05 09:38:45 5050-07-05 09:38:45
2006-01-02 15:04:05
// 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
也就是说,这个时间按代表的部分拆开,其实可以理解为一个递增序列(01 02 03 04 05 06 07)
2006-01-02 15:04:05
为什么用下午三点而不是上午三点?
我盲猜是因为下午时间不能区分,所以写一段代码测试一下:
t, _ := time.Parse("2006-01-02 15:04:05", "2006-01-02 03:04:05") fmt.Println(t) fmt.Println(t.Format("2006-01-02 03:04:05")) fmt.Println(t.Format("2006-01-02 15:04:05")) t, _ = time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05") fmt.Println(t) fmt.Println(t.Format("2006-01-02 03:04:05")) fmt.Println(t.Format("2006-01-02 15:04:05"))
2006-01-02 03:04:052006-01-02 15:04:05
❯ go run simple.go 2006-01-02 03:04:05 +0000 UTC 2006-01-02 03:04:05 2006-01-02 03:04:05 2006-01-02 15:04:05 +0000 UTC 2006-01-02 03:04:05 2006-01-02 15:04:05
2006-01-02 03:04:052006-01-02 15:04:05