[SOLVED] .Date.Format not adding ordinals


#1

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

Glad you asked, could never figure that one out…


#3

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


#4

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.


#5

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


#6

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?


#7

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


#8

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

"January 2nd"
Returns: March 3rd


#9

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”


#10

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


#11

Very cool!

For reference: