Retrieving time units from the date
The Time
type also provides the API to retrieve time units from the instance. This means you are able to find out what day in a month or what hour in a day the instance represents. This recipe shows how to obtain such units.
How to do it...
- Open the console and create the folder
chapter04/recipe05
. - Navigate to the directory.
- Create the
units.go
file with the following content:
package main import ( "fmt" "time" ) func main() { t := time.Date(2017, 11, 29, 21, 0, 0, 0, time.Local) fmt.Printf("Extracting units from: %v\n", t) dOfMonth := t.Day() weekDay := t.Weekday() month := t.Month() fmt.Printf("The %dth day of %v is %v\n", dOfMonth, month, weekDay) }
- Execute the code by running
go run units.go
in the main Terminal. - You will see the following output:

How it works...
The Time
type provides methods to extract time units...