In this Python Article, we will learn how to display the current date and time in Python. In addition, we will learn to customize the format of date and time in Python. Moreover, we will learn the 24-hour and 12-hour clock system in Python.
Python datetime Module
Python provides the built-in ‘datetime’ module for handling date and time. This module provides several classes to manipulate dates and times. Among them, the datetime class is the most important. It includes various functions to work with dates, times, and combined date and times.
Let’s see how to display the current date and time using the datetime module.
import datetime
current_datetime = datetime.datetime.now()
print(current_datetime)
The ‘now’ function of the datetime class returns the current date and time, which we can print to the console. The output of this code will be in the following format: ‘YYYY-MM-DD HH:MM:SS.ssssss’.
Here, ‘YYYY’ denotes a four-digit year, ‘MM’ is a two-digit month, ‘DD’ is a two-digit day, ‘HH’ is a two-digit hour (24-hour format), ‘MM’ is a two-digit minute, ‘SS’ is a two-digit second, and ‘ssssss’ represents microseconds.
Output:
Customizing the DateTime Format in Python
Often, we don’t need the date and time to the microsecond. Additionally, we might want to present the date and time in a different format. Python provides the ‘strftime’ function to format date and time objects into readable strings.
Here’s an example:
import datetime
current_datetime = datetime.datetime.now()
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_datetime)
In this example, ‘%Y-%m-%d %H:%M:%S’ is the format string. Here, ‘%Y’ represents a four-digit year, ‘%m’ a two-digit month, ‘%d’ a two-digit day, ‘%H’ a two-digit hour (24-hour format), ‘%M’ a two-digit minute, and ‘%S’ a two-digit second.
Output:
Displaying Date and Time Separately in Python
In some situations, you might want to display the current date and time separately. Here’s how to do it:
import datetime
current_date = datetime.date.today()
print("Current Date: ", current_date)
current_time = datetime.datetime.now().time()
print("Current Time: ", current_time)
In this code, the ‘today’ function of the date class returns the current date, and the ‘time’ function of the datetime object returns the current time.
Output:
Python program to display current date and time in US standard
In the U.S., the date is typically displayed in the ‘month/day/year’ format, and the time in the 12-hour clock system. If we use the datetime module the output remains the same because it’s showing the default format.
Let’s modify the datetime format to match the U.S. standard:
import datetime
current_datetime = datetime.datetime.now()
formatted_datetime = current_datetime.strftime("%m/%d/%Y, %I:%M:%S %p")
print(formatted_datetime)
Here, ‘%m/%d/%Y, %I:%M:%S %p’ is the format string, where:
%m
is the month as a zero-padded decimal number.%d
is the day of the month as a zero-padded decimal number.%Y
is the year with century as a decimal number.%I
is the hour (12-hour clock) as a zero-padded decimal number.%M
is the minute as a zero-padded decimal number.%S
is the second as a zero-padded decimal number.%p
is either AM or PM according to the given time value.
Output:
Displaying Date and Time Separately in US Standard
The example will show how to display the current date and time separately in US standard or 12-hour standard:
import datetime
current_date = datetime.date.today().strftime("%m/%d/%Y")
print("Current Date: ", current_date)
current_time = datetime.datetime.now().time().strftime("%I:%M:%S %p")
print("Current Time: ", current_time)
In this code, the ‘strftime’ function is used to format the date in the ‘month/day/year’ format and the time in the 12-hour clock system.
Output:
Conclusion
The ‘datetime’ module in Python makes it very easy to get and format the current date and time. It provides various functions that can be used to manipulate date and time data according to the needs of your program.
You may also like the following Python tutorials:
- How to find perfect number in Python
- How to trim a string in Python
- How to reverse a number in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.