Q:

Golang program to print current date-time in different formats using Format() function

belongs to collection: Golang Date & Time Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Date & Time Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang program to get the date, month, year in dif... >>
<< Golang program to get Formatted Date and Time...