今天有人在群里问:code
问一个时间格式化问题: fmt.Println(time.Now().Format("2006year 01month 02day")) 2015year 12month 18day fmt.Println(time.Now().Format("2015year 01month 01day")) 181253year 12month 12day 为何? 困扰很久
首先,这是一个很奇葩的问题。orm
其次,我,以及比我对 Golang 更了解的朋友,都掉过这个坑。咱们曾在这个问题上,花了不少时间,最后发现是 Golang 本身的奇怪设定致使。尤为是,一段时间不用 time 包后,过段时间竟然又忘了。blog
而后,怪咱们没看文档,但是...文档
2006-01-02 15:04:05
示例:get
package main import ( "fmt" "time" ) func main() { fmt.Println("hello ") now := time.Now() nowRight := now.Format("2006-01-02 15:04") fmt.Println(nowRight) nowWrong := now.Format("2006-01-03日错了 15:05 分错了") fmt.Println(nowWrong) fmt.Println(now.Format("Mon Jan _2 15:04:05 2006 年")) fmt.Println("变态吧!") }
附,格式化字符串模板:it
const ( ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" )