How to get current date and time in Python?

In this article, we will show you how you can get the current time and date in Python with the help of different functions. Python provides two main modules for working with date and time: the datetime module and the time module.

Using the Datetime Module

Date and time are not data types in Python, but you can work with them using the datetime module. The datetime module is included in Python, so there is no need to install it separately.

Using the now() Method

The now() method returns the current local date and time ?

import datetime

# Get the current date and time
now = datetime.datetime.now()

# Print the current date and time
print("Current date and time:", now)

# Print the current date
print("Current date:", now.date())

# Print the current time
print("Current time:", now.time())

# Print the current year
print("Current year:", now.year)

# Print the current month
print("Current month:", now.month)
Current date and time: 2025-01-15 14:50:20.666835
Current date: 2025-01-15
Current time: 14:50:20.666835
Current year: 2025
Current month: 1

Using the today() Method

The today() method is similar to now() and returns the current local date and time ?

import datetime

# Get the current date and time
today = datetime.datetime.today()

# Print the current date and time
print("Current date and time:", today)

# Print the current date
print("Current date:", today.date())

# Print the current time
print("Current time:", today.time())

# Print the current year
print("Current year:", today.year)

# Print the current month
print("Current month:", today.month)

# Print the current day
print("Current day:", today.day)
Current date and time: 2025-01-15 14:51:18.615349
Current date: 2025-01-15
Current time: 14:51:18.615349
Current year: 2025
Current month: 1
Current day: 15

Current Date and Time in UTC Format

UTC (Coordinated Universal Time) is a standard time format. Use datetime.now(datetime.timezone.utc) for UTC time ?

import datetime

# Get the date and time in UTC format (recommended approach)
utc_now = datetime.datetime.now(datetime.timezone.utc)

# Date and time in UTC format
print("Current date and time in UTC format:", utc_now)

# Remove timezone info for cleaner display
utc_naive = utc_now.replace(tzinfo=None)
print("UTC time (naive):", utc_naive)
Current date and time in UTC format: 2025-01-15 09:24:45.515400+00:00
UTC time (naive): 2025-01-15 09:24:45.515400

Current Date and Time in ISO Format

ISO format is a standardized string representation of date and time ?

import datetime

# Get the date and time in ISO format
iso_now = datetime.datetime.now().isoformat()

# Print in ISO format
print("Current date and time in ISO format:", iso_now)

# You can also customize the separator
iso_custom = datetime.datetime.now().isoformat(sep=' ')
print("ISO format with space separator:", iso_custom)
Current date and time in ISO format: 2025-01-15T14:56:12.423234
ISO format with space separator: 2025-01-15 14:56:12.423234

Using the Time Module

The time module provides various time-related functions and is also included in Python by default. It works with timestamps (seconds since epoch) and provides formatting capabilities.

Using the time.strftime() Method

The strftime() method formats time into a readable string using format codes ?

import time

# Get the current date and time with custom formatting
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print("Current date and time:", current_time)

# Different format examples
print("Date only:", time.strftime("%Y-%m-%d"))
print("Time only:", time.strftime("%H:%M:%S"))
print("Full format:", time.strftime("%A, %B %d, %Y at %I:%M %p"))
Current date and time: 2025-01-15 14:57:30
Date only: 2025-01-15
Time only: 14:57:30
Full format: Wednesday, January 15, 2025 at 02:57 PM

Using the time.time() Method

The time() method returns the current time as seconds since epoch (January 1, 1970) ?

import time

# Get the current timestamp
current_timestamp = time.time()
print("Current timestamp:", current_timestamp)

# Convert timestamp to readable format
readable_time = time.ctime(current_timestamp)
print("Readable format:", readable_time)

# You can also use time.ctime() directly
direct_time = time.ctime()
print("Direct readable time:", direct_time)
Current timestamp: 1736950818.996939
Readable format: Wed Jan 15 15:00:18 2025
Direct readable time: Wed Jan 15 15:00:18 2025

Current Date and Time in GMT Format

GMT (Greenwich Mean Time) can be obtained using time.gmtime() which returns a structured time object ?

import time

# Get the current date and time in GMT format
gmt_time = time.gmtime()
print("GMT time structure:", gmt_time)

# Format GMT time as string
gmt_formatted = time.strftime("%Y-%m-%d %H:%M:%S GMT", gmt_time)
print("GMT formatted:", gmt_formatted)
GMT time structure: time.struct_time(tm_year=2025, tm_mon=1, tm_mday=15, tm_hour=9, tm_min=32, tm_sec=31, tm_wday=2, tm_yday=15, tm_isdst=0)
GMT formatted: 2025-01-15 09:32:31 GMT

Comparison of Methods

Method Module Returns Best For
datetime.now() datetime datetime object General date/time operations
time.time() time Timestamp (float) Performance timing
time.strftime() time Formatted string Custom formatting
datetime.utcnow() datetime UTC datetime UTC timestamps

Conclusion

Use datetime.now() for most date and time operations as it provides a rich datetime object. Use time.strftime() when you need custom formatting. For UTC time, prefer datetime.now(datetime.timezone.utc) over the deprecated utcnow().

Updated on: 2026-03-24T19:10:10+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements