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.
datetime.now()
from the datetime ModuleThe 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())
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.
time
ModuleThe 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.
date.today()
for Just the DateIf 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.
datetime.utcnow()
for UTC TimeFor 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.
datetime.now(timezone.utc)
for Timezone-Aware UTCTimezone-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.
pytz
for Local Timezone HandlingFor 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.
zoneinfo
from Python 3.9+
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.
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))
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.
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.
Build automated data-cleaning pipelines using Python and Pandas. Learn to handle lost data, remove duplicates, and optimize work
Learn how to build your Python extension for VS Code in 7 easy steps. Improve productivity and customize your coding environment
Explore the top 12 free Python eBooks that can help you learn Python programming effectively in 2025. These books cover everything from beginner concepts to advanced techniques.
Discover how the integration of IoT and machine learning drives predictive analytics, real-time data insights, optimized operations, and cost savings.
What is Python IDLE? It’s a lightweight Python development environment that helps beginners write, run, and test code easily. Learn how it works and why it’s perfect for getting started
Understand ChatGPT-4 Vision’s image and video capabilities, including how it handles image recognition, video frame analysis, and visual data interpretation in real-world applications
AI and misinformation are reshaping the online world. Learn how deepfakes and fake news are spreading faster than ever and what it means for trust and truth in the digital age
concept of LLM routing, approaches to LLM routing, implement each strategy in Python
See which Python libraries make data analysis faster, easier, and more effective for beginners and professionals.
Discover how Adobe's generative AI tools revolutionize creative workflows, offering powerful automation and content features.
Discover three inspiring AI leaders shaping the future. Learn how their innovations, ethics, and research are transforming AI
Discover five free AI and ChatGPT courses to master AI from scratch. Learn AI concepts, prompt engineering, and machine learning.
Explore Idefics2, an advanced 8B vision-language model offering open access, high performance, and flexibility for developers, researchers, and the AI community
Struggling with unpredictable AI output? Learn how improving prompt consistency with structured generations can lead to more reliable, usable, and repeatable results from language models.
Need to get current date and time using Python? This guide walks through simple ways, from datetime and time to pandas and zoneinfo, with clear Python datetime examples.
Discover the most requested ChatGPT features for 2025, based on real user feedback. From smarter memory to real-time web access, see what users want most in the next round of new ChatGPT updates.
Learn 7 effective ways to remove duplicates from a list in Python. Whether you're working with Python lists or cleaning data, these techniques help you optimize your Python code.
Discover how OpenAI's ChatGPT for iOS transforms enterprise productivity, decision-making, communication, and cost efficiency.
Hugging Face enters the world of open-source robotics by acquiring Pollen Robotics. This move brings AI-powered physical machines like Reachy into its developer-driven platform.
Learn how to use numpy.arange() in Python to simplify array creation with custom step sizes and data types. This guide covers syntax, examples, and common use cases.
Discover how to filter lists in Python using practical methods such as list comprehension, filter(), and more. Ideal for beginners and everyday coding.
Discover the key differences between Unix and Linux, from system architecture to licensing, and learn how these operating systems influence modern computing.
Explore FastRTC Python, a lightweight yet powerful library that simplifies real-time communication with Python for audio, video, and data transmission in peer-to-peer apps.
Major technology companies back White House efforts to assess AI risks, focusing on ethics, security, and global cooperation.