日期格式显示错误的日期
I'm trying to format a date like this: [daynumber] [monthname] [fullyear]
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Format("1 January 2014"))
}
However this prints out "11 November 10110" instead of the correct date "29 November 2014".
What is the correct way to use Time.Format
?
我正在尝试格式化这样的日期:[daynumber] [monthname] [fullyear] p>
软件包main
import(
“ fmt”
“ time”
)
func main(){
t:= time.Now()
fmt .Println(t.Format(“ 2014年1月1日”))
}
code> pre>
但是,它打印出的是“ 10110年11月11日”,而不是正确的日期“ 29” 2014年11月”。 p>
使用 Time.Format code>的正确方法是什么? p>
div>
Try:
fmt.Println(t.Format("2 January 2006"))
From Time.Format()
Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time,
Mon Jan 2 15:04:05 -0700 MST 2006
The article "Parsing and formatting date/time in Go " adds:
The use of a mnemonic over obscure formatting codes I think reflects the pragmatism of Go’s developers and their focus on creating a language that makes its users more productive
Ironically, I have trouble remembering the exact values and order of that format template.
(Especially the day and month that I keep mixing up, being used to the dd-mm convention, as opposed to mm-dd).