- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to get the individual components of the current date-time
In this article, we are going to learn how to use various internal time function in Golang that will get the individual components of the current date-time. Now function from the time package will be used to get the current date-time and the individual components of date-time are calculated using the individual components as methods.
Syntax
func Now() Time
The Now() function is defined in time package. This function generates the current local time. To use this function, we have to first import the time package in our program.
time.Month()
This method belongs to the time package. It is used to get the current month as a time.Month value.
time.Day()
This method is a part of the time package. It is used to get the current day as time.Day value.
time.Hour()
This method is a part of the time package. It is used to get the current hour as time.Hour value.
time.Minute()
This method is a part of the time package. It is used to get the current minute as time.Minute value.
time.Second()
This method is a part of the time package. It is used to get the current second as time.Second value.
Using Individual Components
In this example, we will write a Golang program to obtain the current time using individual components. These components will be used with the current time stored in a variable.
Algorithm
Step 1 − Import the required packages in the program
Step 2 − Create a main function
Step 3 − In the main, obtain the current time using built-in function
Step 4 − From the current time retrieve the individual components using internal functions
Step 5 − Print the components using Printf function from fmt package
Example
In the following golang program we will understand how to get individual components of current date-time using individual components
package main import ( "fmt" "time" ) func main() { currentTime := time.Now() year := currentTime.Year() month := currentTime.Month() day := currentTime.Day() hour := currentTime.Hour() minute := currentTime.Minute() second := currentTime.Second() fmt.Println("The individual components can be represented as:") fmt.Printf("Year: %d\n", year) fmt.Printf("Month: %d\n", month) fmt.Printf("Day: %d\n", day) fmt.Printf("Hour: %d\n", hour) fmt.Printf("Minute: %d\n", minute) fmt.Printf("Second: %d\n", second) }
Output
The individual components can be represented as: Year: 2023 Month: 4 Day: 3 Hour: 4 Minute: 31 Second: 59
Using the Date and Clock Method on the Current Time
In this method, we will write a Golang program to get the individual components of current date-time using Date and clock method on current time calculated using the Now method.
Algorithm
Step 1 − This program imports the package fmt and time in the program where fmt helps in the formatting of the input and Output and time helps in getting the time
Step 2 − Create a main function in which calculate the current_time using Now function from the time package
Step 3 − Obtain the individual components of Date, Clock and Zone method on the current time
Step 4 − Print the year, month, day, hour, minute, second and timezone on the console using Println function from the fmt package.
Step 5 − The numbers will be printed on the console using %d format specifier.
Example
The following golang program explains how to get the individual components of current date-time using the Date and clock method on the current time
package main import ( "fmt" "time" ) func main() { current_time := time.Now() year, month, day := current_time.Date() hour, minute, second := current_time.Clock() zone, _ := current_time.Zone() fmt.Printf("Year: %d\n", year) fmt.Printf("Month: %d\n", month) fmt.Printf("Day: %d\n", day) fmt.Printf("Hour: %d\n", hour) fmt.Printf("Minute: %d\n", minute) fmt.Printf("Second: %d\n", second) fmt.Printf("Timezone: %s\n", zone) }
Output
Year: 2023 Month: 4 Day: 3 Hour: 4 Minute: 33 Second: 6 Timezone: UTC
Conclusion
We compiled and executed the program of getting the individual components of current date-time using two examples. In this example, we used the built-in components with the calculated current time and in the second example, we used Date and Clock method to get the individual components of date and time.