Python Date vs. Datetime Objects Explained

Python date is an object that’s used to represent the calendar date with no time of date, while a Python datetime object includes the date and time of day. Here’s how to use them.

Written by Dorothea Reher
Published on Oct. 08, 2024
Software developer coding in python
Image: Shutterstock / Built In
Brand Studio Logo

In Python, date and datetime are classes provided by the datetime module for handling dates and times. Date is useful for representing a calendar date, while a datetime object represents a specific moment in time.

Date in Python Explained

In Python, a date object is used to present a calendar date, while a datetime object represents the date down to the microsecond. Here’s how to code date in Python:

from datetime import datetime

dt = datetime.now()  # Gets the current date and time
print(dt)  # Output will be something like 2023-10-04 15:06:00.12345

Let’s take a look at how each one works with code.

 

Difference Between Python date and datetime Objects 

date Object

A Python date object represents a calendar date (year, month and day) without any time-of-day information.

For example:

from datetime import datetime

dt = datetime.now()  # Gets the current date and time
print(dt)  # Output will be something like 2023-10-04 15:06:00.123456

datetime Object:

A Python datetime object represents a specific moment in time, including both the date and the time, including the year, month, day, hour, minute, second and microsecond).

For example:

from datetime import datetime

dt = datetime.now()  # Gets the current date and time
print(dt)  # Output will be something like 2023-10-04 15:06:00.123456

In summary, use date to represent dates without time, and datetime to represent a specific point in time inclusive of both date and time.

More on PythonTiming Functions in Python: A Guide

 

How to Check Object Type for Python Date and Datetime

To check the type of an object, you can use the built-in type() function or the isinstance() function.

Using type()

from datetime import date, datetime

some_date = date.today()
some_datetime = datetime.now()

print(type(some_date))      # <class 'datetime.date'>
print(type(some_datetime))  # <class 'datetime.datetime'>
A tutorial on Python date and datetime object. | Video: Python Simplified

More on PythonHow to Fix TypeError: ‘List’ Object Is Not Callable in Python

Using isinstance()

from datetime import date, datetime

some_date = date.today()
some_datetime = datetime.now()

print(isinstance(some_date, date))         # True
print(isinstance(some_date, datetime))     # False
print(isinstance(some_datetime, date))     # False
print(isinstance(some_datetime, datetime)) # True

Using isinstance() is usually preferred, especially when dealing with inheritance, because it considers subclasses and provides a more flexible and safe way to check types.

To apply this in  context:

from datetime import date, datetime

# Assuming period.upper is the attribute you want to check
if isinstance(period.upper, date) and not isinstance(period.upper, datetime):
    current_date = date.today()
else:
    current_date = datetime.now()

This way, you ensure that current_date uses date.today() if period.upper is a date object, or timezone.now() if it was a datetime object.

Frequently Asked Questions

Python date represents the calendar date without providing any time of day information. Python datetime represents a specific moment in time, providing both the date and the time down to the microsecond.

Here’s how to code the date using a Python date object:

from datetime import datetime

dt = datetime.now()  # Gets the current date and time
print(dt)  # Output will be something like 2023-10-04 15:06:00.123456

Here’s how to code the date and time using Python datetime object:

from datetime import datetime

dt = datetime.now()  # Gets the current date and time
print(dt)  # Output will be something like 2023-10-04 15:06:00.123456
Explore Job Matches.