Home »
Golang »
Golang Programs
Golang program to print current date-time in different formats using Format() function
Here, we are going to learn how to print current date-time in different formats using Format() function in Golang (Go Language)?
Submitted by Nidhi, on March 23, 2021 [Last updated : March 04, 2023]
How to print the current date-time in different formats in Golang?
Problem Solution:
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.
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.
Golang code to print current date-time in different formats using Format() function
// 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"))
}
Output:
03-23-2021 06:23:22.558712865
03-23-2021
03-23-2021 06:23:22 Tue
03-23-2021 06:23:22
03-23-2021 06:23:22.558712
03-23-2021 06:23:22 Tuesday
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.
Golang Date & Time Programs »