425nesp
425nesp

Reputation: 7583

Date Format shows wrong date

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?

Upvotes: 0

Views: 1124

Answers (1)

VonC
VonC

Reputation: 1323503

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

Upvotes: 1

Related Questions