dongye6377 2014-11-29 21:16
浏览 37
已采纳

日期格式显示错误的日期

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?

  • 写回答

1条回答 默认 最新

  • douzhuo6270 2014-11-29 21:21
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?