Using different formats for dates

Hey,

I see you can use a cool date format with Hugo using natural language, but I can’t figure out how to simply return the YYYY-MM-DD format from the front matter.

When calling using {{ .Date }} I get:
2014-11-25 00:00:00 +0000 UTC

When calling using {{ .Date.Format "YYYY-MM-DD" }} I get:
MMMM-YY-DD

I’m looking for simply 2014-11-25.

Could anybody offer some pointers?

See if this works for you: {{ .Date.Format "2006-01-02" }}

2 Likes

GOOD MAN!

I had tried “2006-01-01” and “2006-01-31” but got weird results.

Nailed it. Thanks very much.

To expand upon @DerekPerkins response; Go uses a model date for formatting, or layout.

Go’s reference time for layouts is:

Mon Jan 2 15:04:05 MST 2006

which can also be expressed as:

01/02 03:04:05PM '06 -0700

For more information: http://golang.org/pkg/time/#pkg-constants

6 Likes

Thanks for the extra @mohae. I hadn’t understood how to output the day of the month part!

“…To expand upon @DerekPerkins response; Go uses a model date for formatting, or layout…”

But why? That is the question.

I got caught out by this too, thinking I could just use an appropriate example for whichever part of the date I wanted. When i put in “2014” as the year format, I got some crazy date about 40000 years into the future.

By picking a default date, there is less parsing that needs to happen, plus they chose a date that shows definitively how to handle things like leading zeroes, day/month ordering and other edge cases that aren’t discernible from all dates.

It’s weird coming from other languages that have you send in formatting strings like “YYYY-MM-DD”, but Go has never concerned itself with doing it the same way as everyone else just because. :slight_smile:

1 Like

I can see where the Go syntax makes it easier in some ways. I’m forever forgetting some of the more obscure formatting settings in other languages that [for example] distinguish between 12/24 hour time or [as you said] include/exclude leading zeroes, etc.

I’m not convinced this solves the problem though. Now, with Go, instead of remembering those obscure switches, I’ve got to remember a complete and [to me] arbitrary date and time instead. And, given the amount of rolling pin dents I have on my head owing to forgetting the missus’s birthday, I don’t think it’s going to prove any easier.

BTW –I did not realise this date formula was a ‘Go’ thing, I thought it was a quirk of Hugo. So thanks for clearing that up, anyway.

dear GOD thank you. This NEEDS to be in the Hugo docs.

+1 for adding this to the Hugo docs somewhere (preferably linking to it from the dateFormat docs somewhere).

I was having the same issue, and it was super confusing trying to figure out what was going on. This was super useful though =)

This should be in the docs, had no clue how to properly set the format to include the time without this forum post!

Hey this is great however I think I encountered a bug when using certain format strings. I am not entire sure why this bug is happening.

Ah nevermind. It turns out I wasn’t using the correct string formatting. For example ‘_2’ = day of the month, 05 is always the seconds and 06 is always the year.

https://golang.org/pkg/time/#Time.Format