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.
Qualcomm expands generative AI offerings through its VinAI acquisition, strengthening on-device AI capabilities for smartphones, cars, and connected devices worldwide.
Nvidia is set to manufacture AI supercomputers in the US for the first time, while Deloitte deepens agentic AI adoption through partnerships with Google Cloud and ServiceNow.
How conversational AI is changing document generation by making writing faster, more accurate, and more accessible. Discover how it works and its implications for the future of communication.
How AI-powered genome engineering is advancing food security, with highlights and key discussions from AWS Summit London on resilient crops and sustainable farming.
Discover how a startup backed by former Google CEO Eric Schmidt is reshaping scientific research with AI agents, accelerating breakthroughs and redefining discovery.
Can smaller AI models outthink their larger rivals? IBM believes so. Here's how its new compact model delivers powerful reasoning without the bulk.
Discover how EY and IBM are driving AI innovations with Nvidia, enhancing contract analysis and reasoning capabilities with smarter, leaner models.
What does GM’s latest partnership with Nvidia mean for robotics and automation? Discover how Nvidia AI is helping GM push into self-driving cars and smart factories after GTC 2025.
Discover how Zoom's innovative agentic AI skills and agents are transforming meetings, customer support, and workflows.
What makes Nvidia's new AI reasoning models different from previous generations? Explore how these models shift AI agents toward deeper understanding and decision-making.
Discover how AI-powered wearable heart monitors are revolutionizing heart health tracking with real-time imaging and analysis, offering insights once limited to hospitals.
Discover how Amazon uses AI to combat fraud across its marketplace. Learn about AI-driven systems that detect and prevent fake sellers, suspicious transactions, and refund scams, enhancing Amazon's fraud prevention.