In Development Last updated:
Share on:
Cloudways offers managed cloud hosting for any size business to host a website or complex web applications.

The Python DateTime class is an important feature of the Python language.

It can be used to create, store, and manipulate dates. This article is a guide on the useful methods of the Python DateTime object used to record dates and perform calculations involving dates.

What is the DateTime Class in Python?

The DateTime module in Python is a built-in object that represents both a date and time. With this object, you can easily parse dates meaningfully and make calculations using them.

It exposes various methods to do this. These methods will be discussed in the sections that follow.

Useful Methods on the DateTime Class

This section will discuss the common methods and properties used on the Python DateTime object. I will also demonstrate these methods with code examples.

To follow along, you must have Python3 installed and a code editor. If you do not have those, you can use our online IDE.

To use dates in Python, you must import the datetime module from the datetime package like so:

from datetime import datetime

The code examples below will assume that you already have that import.


The first and probably most important method in the Python DateTime module is the now method. The now method is a static method that returns the current time in your time zone. For example:

now =

You can print out the object using the print function. This will print out a string representation of the object using the __str__ magic method.


And this will be your output:

The type of this object is a DateTime. You can see this by running the following code:



Any date or time in Python is represented using an object of the DateTime class. However, to display the date or store it in a database, you must convert it to a string. This can be done using the strftime method.

The method takes in one argument, a string representing the format to convert the date to.

For example, you could do the following:

# Creating date, right now
now =

formatted_date = now.strftime('%d/%m/%Y')


In this case, the %d will be substituted with the correct date. The %m will be converted to the correct month, and the %Y will be converted to the correct year. The full list of formatting options can be found <here>:

The result of the code above would be:


While strftime formats a DateTime object into a string, strptime does the opposite. It parses a string into a DateTime object. This method is static, meaning it is available in the class itself. To use it, you pass in two arguments: the string that contains the date and the string that contains the format. For example:

date_string = '4/10/2023'
format_string = '%d/%m/%Y'

parsed_date = datetime.strptime(date_string, format_string)



The isoformat method can be used to format dates into ISO Format 8601. This provides a standard way to format a date without specifying the format as you do with strftime.

now =

iso_formatted_date = now.isoformat()


The strftime and strptime methods can be used to format and parse dates. However, in each case, you must specify the format you are writing to and reading from. This can become cumbersome after some time. If only there were a way to format dates into a standard format from which you can parse them.


The inverse of isoformat is the fromisoformat method. This method will read a date string formatted according to ISO 8601 and parse it into a Python DateTime object. This method is available in the DateTime class and is called with the date_string you want to pass. It will return a Python DateTime object parsed for you.

date_string = '2023-10-04T22:31:52.346060'

parsed_date = datetime.fromisoformat(date_string)



The DateTime object stores a date and time in a collection of properties. Example properties would be month and year. You can use methods like the ones demonstrated below to access these properties.

now =

year = now.year()
month = now.month()

However, oftentimes, you would want to access all these properties at once. That is what the timetuple method is for. It returns the DateTime object as a tuple. The order of the values in the tuple is expressed in the table below:

1Month of the Year (1 – 12)
2Date of the Month (1 – 31)
3Hour of the Day (0 – 23)
4Minute of the Hour (0 – 59)
5Second of the Minute (0 – 59)
6Day of the Week (0 – 6) where 0 represents Monday, and 6 Sunday
7Day of the Year (1 – 366) where 1 is Jan 1st
8Daylight Savings Time

Here’s an example where we get the timetuple and print it out.

time_tuple = now.timetuple()



The timedelta class is technically different from the DateTime class. However, it is important so that we will cover it here. The best way to explain is to demonstrate, so here is an example.

from datetime import datetime, timedelta

# Creating a date now
now =

# Creating a time delta for one day
change = timedelta(days=1)

# Adding a time delta to today
tomorrow = now + change


As you can see from the code snippet above, change it is a timedelta object that represents a change in one day. This timedelta object can be added to a DateTime object to get tomorrow’s date. However, you can also subtract it to get yesterday’s date before.


In this article, we covered the DateTime Class in Python and some of the most useful methods on the DateTime objects.

Next, check out our article on the best Python Cheat Sheets.

Share on:
  • Anesu Kafesu
    I am a Software Engineer specialising in Web Development, Mobile Application Development and Artificial Intelligence. I occasionally write on the same topics.
  • Rashmi Sharma

    Rashmi is a highly experienced content manager, SEO specialist, and data analyst with over 7 years of expertise. She has a solid academic background in computer applications and a keen interest in data analysis.

    Rashmi is…

Thanks to our Sponsors

More great readings on Development

Power Your Business

Some of the tools and services to help your business grow.
  • The text-to-speech tool that uses AI to generate realistic human-like voices.

    Try Murf AI
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.

    Try Brightdata
  • is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.

    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.

    Try Intruder