Ani
Ani

Reputation: 119

How do i convert a number to a month in Go

I just want to take an integer input and convert it into corresponding month using Go time package. Is there a way apart from defining months in using a const block and using iota to incrementally represent them ?

Upvotes: 1

Views: 2239

Answers (1)

dave
dave

Reputation: 64695

You can use the type time.Month, which implements the Stringer interface, which means you can do something like:

m := time.Month(10)
fmt.Println(m) //"October" - could also do m.String() here
fmt.Println(int(m)) //10

https://play.golang.org/p/PeFfVZZIK_

Upvotes: 6

Related Questions