Learn about Program to get current date and time in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Program to get current date and time in Golang
// Golang program to get the current date and time
package main
import "fmt"
import "time"
func main() {
var currentDateAndTime string
currentDateAndTime = time.Now().String()
fmt.Println("Current date and time\n", currentDateAndTime)
}
Here, we created a string currentDateTime. Then we called the time.Now() function to get the current date and time. The time.Now() function return the object of the time type, here we used String() function to convert the time into the string.
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Program to get the day of the specified date in Golang
Program to convert a date-time object into a string in Golang