In this program, we will get the current date-time using the Now() function and print date-time in different formats using the Format() function on the console screen.
The source code to print current date-time in different formats using the Format() function is given below. The given program is compiled and executed successfully.
// Golang program to print current date-time in
// different formats using Format() function
package main
import "fmt"
import "time"
func main() {
dateTime := time.Now()
fmt.Println(dateTime.Format("01-02-2006 15:04:05.000000000"))
fmt.Println(dateTime.Format("01-02-2006"))
fmt.Println(dateTime.Format("01-02-2006 15:04:05 Mon"))
fmt.Println(dateTime.Format("01-02-2006 15:04:05"))
fmt.Println(dateTime.Format("01-02-2006 15:04:05.000000"))
fmt.Println(dateTime.Format("01-02-2006 15:04:05 Monday"))
}
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt and time packages to use time and fmt related functions.
In the main() function, we used the Now() function to get current date-time and then printed date-time in different formats using the Format() function on the console screen.
Program/Source Code:
The source code to print current date-time in different formats using the Format() function is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt and time packages to use time and fmt related functions.
In the main() function, we used the Now() function to get current date-time and then printed date-time in different formats using the Format() function on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer