-3

I'm trying to get the day as a string from a time.Now() instance.

now := time.Now() // .String() would give me the entire date as a string which I don't need
day := now.Day()) // is what I want but as a String.

So string(day) tells me "can not convert day to string".

For me now.Day().String() would be nice but there is no such method...

I could now try to take time.Now().String() and manipulate until the day is left over. But there should be a easier way to do it...

Jurudocs
  • 8,595
  • 19
  • 64
  • 88

2 Answers2

2

Use strconv to convert int to string

strconv.Itoa(day)
KibGzr
  • 2,053
  • 14
  • 15
-1

You can import and use strconv as KibGzr mentioned. Just to give a complete example:

package main

import (
    "fmt"
    "time"
    "strconv"
)

func main() {
    now := time.Now() 
    day := now.Day()
    fmt.Printf("%T\n",(day))
    fmt.Println(strconv.Itoa(day))
    dayString := strconv.Itoa(day)
    fmt.Printf("%T",(dayString))
}

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

Satyajit Das
  • 2,740
  • 5
  • 16
  • 30