Day, month and year can be extracted from time.Time using Date() . It will return ints for day and year and time.Month for the month. You can also extract the Hour, Minute, and Second values ββusing the Clock() method, which returns an int for all results.
For example:
package main import ( "fmt" "time" ) func main() { t := time.Now() y, mon, d := t.Date() h, m, s := t.Clock() fmt.Println("Year: ", y) fmt.Println("Month: ", mon) fmt.Println("Day: ", d) fmt.Println("Hour: ", h) fmt.Println("Minute: ", m) fmt.Println("Second: ", s) }
Remember that the Month ( mon ) variable is returned as time.Month , and not as a string or int. You can print it using fmt.Print() since it has a String() method.
Playground
Intermernet
source share