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

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






Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.