In this section, we are going to learn how to find today's date and time. We can use time.Now
to get today's date, and it imports a time
package, and time
returns a time
type, and so we are going to assign this to another variable and use the String
function. The following code will help you understand better:
package main import ( "time" "fmt" ) func main(){ current := time.Now() fmt.Println(current.String()) }
The output for the preceding code is as follows:
As you can see, we get a big string here that has everything, and we can, of course, format that to our needs. For instance, I can add the current.Format
function and a predefined layout as seen in the following screenshot:
package main import ( "time" "fmt" ) func main(){ current := time.Now() fmt.Println(current.String()) fmt.Println("MM-DD-YYYY :", current.Format("01-02-2006")) }
The output to the code shown in the preceding screenshot is as follows:
In the previous screenshot, you will...