Python Current Date Time – Tutorial

In Python, we can use the datetime module to get the current date and time of the local system.

To retrieve the current date and time, use the datetime.now() function:

from datetime import datetime

# Current date time in local system
print(datetime.now())


Output: 2025-01-21 10:17:56.456080

Note: datetime.now() returns a naive datetime object that does not include timezone information. For applications that require time zones, you should use the zoneinfo library.

Python Current Date

If you’re only interested in the date, you can use the date() method of the datetime object:

print(datetime.date(datetime.now()))

Output: 2025-01-21

Python Current Time

If you only need the time, use the time() method of the datetime object:

print(datetime.time(datetime.now()))

Output: 14:19:46.423440

Time Zones with the zoneinfo Module

Since Python 3.9, the zoneinfo module is part of the standard library and allows you to work with time zones based on the IANA time zone database.

To get the current time in a specific time zone, you can do the following:

from datetime import datetime
from zoneinfo import ZoneInfo # Available from Python 3.9

# Define time zones
utc = ZoneInfo(“UTC”)
pst = ZoneInfo(“America/Los_Angeles”)
ist = ZoneInfo(“Asia/Kolkata”)

# Current date and time in different time zones
now_utc = datetime.now(tz=utc)
now_pst = datetime.now(tz=pst)
now_ist = datetime.now(tz=ist)

print(‘Current Date and Time in UTC:’, now_utc)
print(‘Current Date and Time in PST:’, now_pst)
print(‘Current Date and Time in IST:’, now_ist)


Output:

Current Date and Time in UTC: 2025-01-21 07:12:16.886000+00:00
Current Date and Time in PST: 2025-01-21 00:12:16.886000-08:00
Current Date and Time in IST: 2025-01-21 12:42:16.886000+05:30

Using the pytz Module for Time Zones

Let’s see some examples of using the pytz module to get the time in specific time zones:

import pytz
from datetime import datetime

utc = pytz.utc
pst = pytz.timezone(‘America/Los_Angeles’)
ist = pytz.timezone(‘Asia/Calcutta’)

print(‘Current Date Time in UTC =’, datetime.now(tz=utc))
print(‘Current Date Time in PST =’, datetime.now(tz=pst))
print(‘Current Date Time in IST =’, datetime.now(tz=ist))


Output:

Current Date Time in UTC = 2018-09-12 08:57:18.110068+00:00 
Current Date Time in PST = 2018-09-12 01:57:18.110106-07:00 
Current Date Time in IST = 2018-09-12 14:27:18.110139+05:30

Note: The zoneinfo module uses the system’s time zone data. On some systems, especially Windows, these data may not be available. In such cases, you can install the tzdata package from PyPI to provide the necessary time zone data.

Python Pendulum Module

Pendulum is another library for easy handling of dates and times in Python. It offers a user-friendly API and handles time zones correctly.

You can install Pendulum with the following command:

An example of using Pendulum to work with time zones:

import pendulum

utc = pendulum.timezone(‘UTC’)
pst = pendulum.timezone(‘America/Los_Angeles’)
ist = pendulum.timezone(‘Asia/Calcutta’)

print(‘Current Date Time in UTC =’, pendulum.now(utc))
print(‘Current Date Time in PST =’, pendulum.now(pst))
print(‘Current Date Time in IST =’, pendulum.now(ist))

Output:

Current Date Time in UTC = 2025-01-20 09:07:20.267774+00:00 
Current Date Time in PST = 2025-01-20 02:07:20.267806-07:00 
Current Date Time in IST = 2025-01-20 14:37:20.267858+05:30

Important: Since version 3.0.0, Pendulum only supports Python versions 3.8 and above. Ensure your Python version is compatible.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: