Formatting date to string
In case the textual representation of a time value is needed, usually, certain formatting is expected. The Time
type of the time
package provides the ability to create the string
output in the given format. There are some rules on how to do this and we will cover a few useful ones.
How to do it...
- Open the console and create the folder
chapter04/recipe02
. - Navigate to the directory.
- Create the
format.go
file with the following content:
package main import ( "fmt" "time" ) func main() { tTime := time.Date(2017, time.March, 5, 8, 5, 2, 0, time.Local) // The formatting is done // with use of reference value // Jan 2 15:04:05 2006 MST fmt.Printf("tTime is: %s\n", tTime.Format("2006/1/2")) fmt.Printf("The time is: %s\n", tTime.Format("15:04")) //The predefined formats could // be used fmt.Printf("The time is: %s\n", tTime.Format...