- 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 display current date and time
In Golang we can display current date and time using “now and format function”, “now function”, and “strconv package” to display the current date and time. In this article we are going to learn how to create a golang program to display current date and time using various Examples.
Syntax
funcNow() 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.
strconv.Quote()
This function belongs to the strconv package. It’s goal is to return the output in quotes with proper escape and backslashes. It takes the string as the input.
time.Format()
This function is present in the time package. It takes a layout input string that depicts how the output string will be formatted.
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 and the date using the built-in function
Step 4 − Print the current time and the date using fmt package’s Println function
Example 1
In this Example, we will use Now function from the time package to obtain the current time and the date. We will further make use of Format function on the current time to obtain a particular format of date and time.
package main import ( "fmt" "time" ) func main() { current_time := time.Now() fmt.Println("Current time along with the date is:", current_time.Format("2006-01-02 15:04:05")) }
Output
Current time along with the date is: 2009-11-10 23:00:00
Example 2
In this illustration, the current time and the date is obtained using Now function from the time package. This function will return the time struct which will determine the format of the current date and time.
package main import ( "fmt" "time" ) func main() { current_time := time.Now() fmt.Println("Current Time and date is:", current_time) }
Output
Current Time and date is: 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
Example 3
In this particular Example, the output will be printed in quotes using the Quote function from strconv package before that the current time will be converted to string using String function.
package main import ( "fmt" "strconv" "time" ) func main() { current_time := time.Now() fmt.Println("Current Time and date is:", strconv.Quote(current_time.String())) }
Output
Current Time and date is: "2009-11-10 23:00:00 +0000 UTC m=+0.000000001"
Conclusion
We executed the program of displaying the current date and time. In the first Example we used Now function from the time package with formatting of date, in the second Example we only used Now function and in the third Example we used Quote function along with the Now function.