How to Get the Current Date and Time in Python?

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 datetime

By 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.883296

In 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:

how to get current date and time in python

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 PM

In 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:

print date in python

You can customize the format string according to your needs using the available directives. Here are some commonly used directives:

DirectiveDescription
%YYear with century (e.g., 2023)
%mMonth as a zero-padded decimal number (01-12)
%dDay of the month as a zero-padded decimal number (01-31)
%HHour (24-hour clock) as a zero-padded decimal number (00-23)
%IHour (12-hour clock) as a zero-padded decimal number (01-12)
%MMinute as a zero-padded decimal number (00-59)
%SSecond as a zero-padded decimal number (00-59)
%pLocale’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 pytz

Once 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:00

In 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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.