You're actually pretty close :) Then return value from time.Now()
is a Time
type, and looking at the package docs here will show you some of the methods you can call (for a quicker overview, go here and look under type Time
). To get each of the attributes you mention above, you can do this:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Month())
fmt.Println(t.Day())
fmt.Println(t.Year())
}
If you are interested in printing the Month
as an integer, you can use the Printf
function:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Printf("%d
", t.Month())
}