Published on Jun 11, 2025 4 min read

All You Need to Know About Getting Date and Time in Python

When working with Python, you’ll often find yourself needing to know what time it is—literally. Whether you’re building a log system, scheduling tasks, or tagging a file with a timestamp, grabbing the current date and time is a common task.

Python offers various ways to achieve this. Some methods are part of the built-in standard library, while others come from external packages. Each has its quirks and advantages, depending on your needs. Let’s explore practical ways to get the current date and time in Python.

How to Get Current Date and Time Using Python

Using datetime.now() from the datetime Module

The datetime module is part of Python’s standard library and doesn’t require extra installation.

from datetime import datetime

now = datetime.now()
print("Current Date and Time:", now)

This prints the current local date and time down to microseconds, like this:

Current Date and Time: 2025-06-04 14:37:53.418997

To get just the date or time separately, use:

print("Date:", now.date())
print("Time:", now.time())

Using datetime.today()

Datetime Today

datetime.today() is similar to datetime.now(), but it’s a bit simpler. It provides the local current date and time.

from datetime import datetime

today = datetime.today()
print("Today:", today)

Functionally, it works much like datetime.now() when no timezone is specified.

Using the time Module

The time module is older and lower-level than datetime. It returns the time as a structured object or timestamp.

import time

current_time = time.localtime()
print("Current Time:", time.strftime("%Y-%m-%d %H:%M:%S", current_time))

This gives you a formatted date and time string, useful when you need a string output immediately without a full datetime object.

Using date.today() for Just the Date

If you only need today’s date without the time, this method is cleaner.

from datetime import date

today_date = date.today()
print("Today's Date:", today_date)

This is ideal for logging or tagging files with a date, skipping the time part altogether.

Using datetime.utcnow() for UTC Time

For standardized time across different zones, such as for databases or APIs, UTC is ideal.

from datetime import datetime

utc_now = datetime.utcnow()
print("Current UTC Time:", utc_now)

Remember, utcnow() doesn’t include timezone information. For a timezone-aware version, see the next method.

Using datetime.now(timezone.utc) for Timezone-Aware UTC

Timezone-aware datetimes are more precise, especially when dealing with different time zones.

from datetime import datetime, timezone

aware_utc = datetime.now(timezone.utc)
print("Timezone-Aware UTC:", aware_utc)

This adds a +00:00 at the end, showing it’s explicitly using UTC.

Using pytz for Local Timezone Handling

For specific timezones, such as Asia/Kolkata or America/New_York, pytz is helpful.

First, install it if needed:

pip install pytz

Then:

from datetime import datetime
import pytz

tz = pytz.timezone('Asia/Kolkata')
local_time = datetime.now(tz)
print("Time in Asia/Kolkata:", local_time)

Note: While pytz is still widely used, it’s recommended to use the zoneinfo module in new projects.

Using zoneinfo from Python 3.9+

Zoneinfo Usage

If you’re using Python 3.9 or later, you can utilize the built-in zoneinfo module for timezone-aware datetimes.

from datetime import datetime
from zoneinfo import ZoneInfo

dt = datetime.now(ZoneInfo("America/New_York"))
print("New York Time:", dt)

This is the recommended modern way to handle time zones.

Using the calendar Module with time

The calendar module isn’t specifically made for getting the current date and time, but it can be combined with time for useful information.

import time
import calendar

timestamp = time.time()
print("Unix Timestamp:", timestamp)
print("Current Year:", time.localtime().tm_year)
print("Is it a Leap Year?", calendar.isleap(time.localtime().tm_year))

Using pandas.Timestamp.now()

If you’re already using the pandas library for data work, it provides a simple way to get the current time as a Timestamp object.

import pandas as pd

now = pd.Timestamp.now()
print("Current Timestamp using pandas:", now)

This is timezone-aware by default if your system supports it.

Conclusion

Getting the current date and time in Python can be achieved in many ways, each suited to different needs. Whether you need a simple timestamp, a timezone-aware datetime, or formatted output, Python’s standard libraries and external packages offer solid options. From datetime.now() for quick local time to zoneinfo for handling time zones cleanly, and even tools like pandas for data-focused tasks, there’s a method for every use case. Understanding these options helps avoid confusion and makes working with dates and times smoother. Pick the right approach based on your project requirements and the precision or flexibility needed for time handling.

For more advanced tutorials, check out the official Python documentation for further reading and examples.

Related Articles

Popular Articles