As a Python developer, you will get to work on dates and times in many projects. So, I thought to write a complete tutorial on how to get the current date and time in Python using the built-in datetime module.
Import the Python datetime Module
To get started, we need to import the datetime module in our Python code. This module provides classes for working with dates and times. You can import it using the following statement:
from datetime import datetimeBy importing the datetime class from the datetime module, we can easily access the current date and time in a Python program or code.
Check out Convert DateTime to UNIX Timestamp in Python
Get the Current Date and Time in Python
Now, let me show you how to obtain the current date and time using the datetime class. The now() method of the datetime class returns the current local date and time. Here’s an example:
current_datetime = datetime.now()
print("Current date and time:", current_datetime)Output:
Current date and time: 2024-12-17 22:51:38.883296In this example, we create an instance of the datetime class using the now() method and store it in the current_datetime variable. When we print current_datetime, it displays the current date and time in the format YYYY-MM-DD HH:MM:SS.microseconds.
I executed the above Python code, and you can see the exact output in the screenshot below:

Check out How to Convert Int to Datetime Python
Format the Date and Time in Python
Sometimes, you may need to format the date and time according to your specific requirements. The datetime class provides the strftime() method, which allows you to format the date and time using various directives.
Let’s consider an example where we want to display the date in the format “Month Day, Year” and the time in the 12-hour format:
current_datetime = datetime.now()
formatted_date = current_datetime.strftime("%B %d, %Y")
formatted_time = current_datetime.strftime("%I:%M:%S %p")
print("Current date:", formatted_date)
print("Current time:", formatted_time)Output:
Current date: December 17, 2024
Current time: 10:54:04 PMIn this example, we use the strftime() method to format the date and time. The %B directive represents the full month name, %d represents the day of the month, and %Y represents the year. For the time, %I represents the hour in 12-hour format, %M represents the minutes, %S represents the seconds, and %p represents the AM/PM designation.
Here is the exact output:

You can customize the format string according to your needs using the available directives. Here are some commonly used directives:
| Directive | Description |
|---|---|
%Y | Year with century (e.g., 2023) |
%m | Month as a zero-padded decimal number (01-12) |
%d | Day of the month as a zero-padded decimal number (01-31) |
%H | Hour (24-hour clock) as a zero-padded decimal number (00-23) |
%I | Hour (12-hour clock) as a zero-padded decimal number (01-12) |
%M | Minute as a zero-padded decimal number (00-59) |
%S | Second as a zero-padded decimal number (00-59) |
%p | Locale’s equivalent of either AM or PM |
Read Convert Python String to Datetime with Timezone
Work with Time Zones in Python
By default, the datetime.now() method returns the current date and time based on your local time zone. However, if you need to work with different time zones, you can use the pytz module. It allows you to create datetime objects with specific time zone information.
First, install the pytz module by running the following command:
pip install pytzOnce installed, you can use it to retrieve the current date and time in a specific time zone. Here’s an example:
from datetime import datetime
import pytz
# Get the current date and time in the US Eastern Time Zone
eastern_tz = pytz.timezone('US/Eastern')
current_datetime = datetime.now(eastern_tz)
print("Current date and time in US/Eastern:", current_datetime)
# Get the current date and time in the US Pacific Time Zone
pacific_tz = pytz.timezone('US/Pacific')
current_datetime = datetime.now(pacific_tz)
print("Current date and time in US/Pacific:", current_datetime)Output:
Current date and time in US/Eastern: 2024-12-18 10:30:45.123456-04:00
Current date and time in US/Pacific: 2024-12-18 07:30:45.123456-07:00In this example, we create datetime objects with specific time zones using the pytz.timezone() function. We pass the desired time zone identifier as a string argument (e.g., ‘US/Eastern’ or ‘US/Pacific’). The resulting datetime object includes the time zone information, and when printed, it displays the date and time adjusted to the specified time zone.
Conclusion
In this tutorial, I have explained how to get the current date and time in Python using the datetime module. We covered the basics of retrieving the current date and time, formatting the output, working with time zones, etc.
As a Python developer, you should know how to work with dates and times for various Python applications, such as logging, data analysis, and web development. The datetime module provides a convenient way to handle date and time operations in Python.
I hope this tutorial helps you find the current date and time in Python.
You may also like:

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.