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.
Hyundai creates new brand to focus on the future of software-defined vehicles, transforming how cars adapt, connect, and evolve through intelligent software innovation.
Discover how Deloitte's Zora AI is reshaping enterprise automation and intelligent decision-making at Nvidia GTC 2025.
Discover how Nvidia, Google, and Disney's partnership at GTC aims to revolutionize robot AI infrastructure, enhancing machine learning and movement in real-world scenarios.
What is Nvidia's new AI Factory Platform, and how is it redefining AI reasoning? Here's how GTC 2025 set a new direction for intelligent computing.
Can talking cars become the new normal? A self-driving taxi prototype is testing a conversational AI agent that goes beyond basic commands—here's how it works and why it matters.
Hyundai is investing $21 billion in the U.S. to enhance electric vehicle production, modernize facilities, and drive innovation, creating thousands of skilled jobs and supporting sustainable mobility.
An AI startup hosted a hackathon to test smart city tools in simulated urban conditions, uncovering insights, creative ideas, and practical improvements for more inclusive cities.
Researchers fine-tune billion-parameter AI models to adapt them for specific, real-world tasks. Learn how fine-tuning techniques make these massive systems efficient, reliable, and practical for healthcare, law, and beyond.
How AI is shaping the 2025 Masters Tournament with IBM’s enhanced features and how Meta’s Llama 4 models are redefining open-source innovation.
Discover how next-generation technology is redefining NFL stadiums with AI-powered systems that enhance crowd flow, fan experience, and operational efficiency.
Gartner forecasts task-specific AI will outperform general AI by 2027, driven by its precision and practicality. Discover the reasons behind this shift and its impact on the future of artificial intelligence.
Hugging Face has entered the humanoid robots market following its acquisition of a robotics firm, blending advanced AI with lifelike machines for homes, education, and healthcare.