1

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.

Frezzle
  • 29
  • 1
  • 5

2 Answers2

3

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.

Cosmic Ossifrage
  • 4,977
  • 29
  • 30
0

It's not as pretty as it could be but you could use this:

func GetOrdinalNumber(n int) string {
    func GetOrdinalNumber(n int) string {
    if n >= 11 && n <= 13 {
        return fmt.Sprintf("%dth", n)
    }

    switch n % 10 {
    case 1:
        return fmt.Sprintf("%dst", n)
    case 2:
        return fmt.Sprintf("%dnd", n)
    case 3:
        return fmt.Sprintf("%drd", n)
    default:
        return fmt.Sprintf("%dth", n)
    }
}
Jeff
  • 11
  • 2