Golang program to print individual elements of date and time using the inbuilt function

Here, we are going to learn how to print individual elements of date and time using the inbuilt function in Golang (Go Language)?
Submitted by Nidhi, on March 21, 2021

Problem Solution:

In this program, we will get the current date and time using time.Now() function. Then we get the elements of date and time using inbuilt functions.

Program/Source Code:

The source code to print individual elements of date and time using the inbuilt function is given below. The given program is compiled and executed successfully.

// Golang program to print individual elements of date and
// time using the inbuilt function

package main

import "fmt"
import "time"

func main() {
	//Get the current date and time.
	currentDateTime := time.Now()

	day := currentDateTime.Day()
	month := currentDateTime.Month()
	year := currentDateTime.Year()

	hour := currentDateTime.Hour()
	min := currentDateTime.Minute()
	sec := currentDateTime.Second()

	fmt.Printf("\nDay      : %d", day)
	fmt.Printf("\nMonth    : %d", month)
	fmt.Printf("\nYear     : %d", year)
	fmt.Printf("\nHour     : %d", hour)
	fmt.Printf("\nMinute   : %d", min)
	fmt.Printf("\nSecond   : %d", sec)
}

Output:

Day      : 21
Month    : 3
Year     : 2021
Hour     : 6
Minute   : 24
Second   : 42

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 package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we called time.Now() to get the current date and time. After that, we get the individual elements of date and time using built-in functions and print the result 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.