[SOLVED] .Date.Format not adding ordinals

I’m using .Format to format a date in my status template:

<h1>{{ .Date.Format "Monday, 2nd January 2006" }}</h1>

However, it seems that ordinals aren’t being rendered, eg the date comes out as:

Tuesday, 27nd February 2018.

I’ve tried quite a few variations and can’t seem to find a way round it :thinking:

2 Likes

Glad you asked, could never figure that one out…

1 Like

Seems the docs are a bit confusing because the ordinal output is listed, but further down the page it says they are not supported.

1 Like

Not sure if this is the exact right place, but it doesn’t look like it exists in Go https://github.com/golang/go/blob/master/src/time/format.go#L88

Also, here’s a helpful formatter: http://fuckinggodateformat.com/

Here’s another resource: https://gohugohq.com/howto/hugo-dateformat/

You could probably work up something to handle this. I did it with Jekyll once.

2 Likes

ack. not a deal breaker, just a pain in the bum :disappointed:

That’s useful, thanks for links. Cool name for helpful formatter :sunglasses:, puts me in mind of http://ohshitgit.com/

Just as an aside, how come it doesn’t exist in Go? Is it because it’s a lot of work to implement these things and Go is (relatively) not so widespread?

I’m not sure. I remember seeing an issue about it some time ago. Might have had something to do with languages.

2 Likes

I’ll make a pull request on the docs to remove

“January 2nd”
Returns: March 3rd

2 Likes

You can use the humanize function:

this:

{{ .Date.Format "January 2, 2006" }}

returns “January 2, 2006”

this:

{{ .Date.Format "January" }}
{{ .Date.Format "2" | humanize}},
{{ .Date.Format "2006" }}

returns “January 2nd, 2006”

6 Likes

humanize function, that is so cool I will have to use it straight away! Thank you :slightly_smiling_face:
It works! Brill :smile:

Very cool!

For reference:

2 Likes