datetimegotimeformatordinal

How to get ordinal indicators (1st, 2nd, 3rd, etc) in Go


Go's time package lets me do this:

time.Now().Format("2 January 2006")

which returns something like 10 November 2009.

Can I get something like 10th November 2009 using time or any standard Go package?

The time.Format documentation doesn't mention it, and I'd like to avoid doing it manually if possible.


Solution

  • There's no support for that in the built-in time library. You'll either need to implement the functionality yourself, using an algorithm as described in the linked Java question, or use a third-party library which can do it for you, such as humanize: https://godoc.org/github.com/dustin/go-humanize#Ordinal.