Signup/Sign In

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:

  1. Date and time in Separated format

  2. Date and Time in 12-hour 24-hour format

  3. Date in different formats

Let us first get familiar with the input-output:

Approach-1

Datetime

Approach-2

Datetime2

Approach-3

date3

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

  1. import datetime module
  2. use now() to get the current time
  3. print the day
  4. print the month
  5. print the year
  6. Print the hour
  7. Print the minute
  8. Print the second
  9. 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

  1. Import datetime
  2. Declare now with strftime function
  3. Declare hr for 24-hour format
  4. Declare hour for 12-hour format
  5. 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

  1. from datetime import date
  2. Print the first format with strfitime
  3. Print the second format with strfitime
  4. Print the third format with strfitime
  5. 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.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.