Get Current Date and Time using Python
In this tutorial, we will create a program that will give us the current date and time. The program can be done with three different methods which we will be covering in this tutorial.
The two different approaches are as follows:
-
Date and time in Separated format
-
Date and Time in 12-hour 24-hour format
-
Date in different formats
Let us first get familiar with the input-output:
Approach-1
Approach-2
Approach-3
Let us now dive deep into the approaches one by one:
Approach 1 - Date and time in Separated format
In this approach, we will import datetime module and print the current time in the format of "Day", "Month", "Year", "Hour", "Minute", "Second", "Microsecond". Let us now see the algorithm:
Algorithm
- import datetime module
- use now() to get the current time
- print the day
- print the month
- print the year
- Print the hour
- Print the minute
- Print the second
- Print the microsecond
Python Program
import datetime
time = datetime.datetime.now()
print ("The current date and time are: ")
print ("Day : ", end = "")
print (time.day)
print ("Month : ", end = "")
print (time.month)
print ("Year : ", end = "")
print (time.year)
print ("Hour : ", end = "")
print (time.hour)
print ("Minute : ", end = "")
print (time.minute)
print ("Second : ", end = "")
print (time.second)
print ("Microsecond : ", end = "")
print (time.microsecond)
The current date and time are:
Day: 23
Month: 8
Year: 2021
Hour: 17
Minute: 9
Second: 41
Microsecond: 588318
Approach 2 -Date and Time in 12-hour 24-hour format
In this approach, we will import datetime and print the current time in the format of "YEAR: MONTH: DAY: HOUR: MINUTE: SECOND" format in two different formats of 12-hour and 24 hours format. Let us now see the algorithm:
Algorithm
- Import datetime
- Declare now with strftime function
- Declare hr for 24-hour format
- Declare hour for 12-hour format
- Print the date and time
Python Program
from datetime import datetime
now = datetime.now()
hour=now.strftime('%Y/%m/%d %H:%M:%S')
hr=now.strftime('%Y/%m/%d %I:%M:%S')
print("The 24hr format is: ", hour)
print("The 12hr format is: ", hr)
The 24hr format is: 2021/08/23 17:34:40
The 12hr format is: 2021/08/23 05:34:40
Approach 3 - Date in different formats
In this approach, we will import date and print dates in 4 different formats. The first format is dd/mm/yyyy, the second format is mm/dd/yy, the third format is Alphabetical year and the fourth format is abbreviated year format.
Let us now see the algorithm followed by the program:
Algorithm
- from datetime import date
- Print the first format with strfitime
- Print the second format with strfitime
- Print the third format with strfitime
- Print the fourth format with strfitime
Program
from datetime import date
now = date.today()
date1 = now.strftime("%d/%m/%Y")
print("date =", date1)
date2 = now.strftime("%m/%d/%y")
print("date =", date2)
date3 = now.strftime("%B %d, %Y")
print("date =", date3)
date4 = now.strftime("%b-%d-%Y")
print("date =", date4)
date = 23/08/2021
date = 08/23/21
date = August 23, 2021
date = Aug-23-2021
Conclusion
In this tutorial, we will perform a program to get the current time with two different approaches. The first approach is by printing Date and time in Separated formats, the second approach is by printing Date and Time in 12-hour 24-hour format and the last approach is by printing Date in different formats.