Convert DD/MM/YY To Date In Python (Examples)

How do you convert a string in the format of DD/MM/YY to a date in Python?

To convert a string in the format of DD/MM/YY to a date in Python import the datetime library and use the datetime.datetime.strptime(date_string, format_string) function (yes, that is not a typo the datetime library has a datetime class and it contains the strptime function).

To make it easier to access the strptime() function it helps to import the datetime class when the datetime library is imported, like so: from datetime import datetime .

Here’s an example of a string in the format DD/MM/YY and how it can be converted to a date in Python using the REPL:

>>> from datetime import datetime
>>> d = "31/07/22"
>>> datetime.strptime(d, "%d/%m/%y")
datetime.datetime(2022, 7, 31, 0, 0)

As you can see from the above result the strptime() function takes the string to convert into a datetime object and according to the second parameter changes the string due to the syntax of day of the month %d , the month of the year %m and the abbreviated 2-digit year %y .

As the result is a datetime object you can further amend the code so that the result just returns a date object. Here’s how this would look in the REPL:

>>> from datetime import datetime
>>> d = "31/07/22"
>>> datetime.strptime(d, "%d/%m/%y").date()
datetime.date(2022, 7, 31)

As you can see from the above code the result outputs a date object instead of a datetime object which previously contained hour and minute of the day.

ValueError When Using strptime

If you do encounter a ValueError when trying to convert a string into a date object then check both parameters of the strptime separately. Check the string being inserted into the first parameter is an actual date and then check the format string represents the format of the string being inserted.

For example, the following examples produce an error:

>>> from datetime import datetime
>>> d = "31/06/22"
>>> datetime.strptime(d, "%d/%m/%y").date()
ValueError: day is out of range for month

The reason for the ValueError in the above code is due to the original input string representing a date that does not exist: 31st June 2022 is not found on the Julian calendar!

Here’s another ValueError , see if you can spot the problem:

>>> from datetime import datetime
>>> d = "31/07/22"
>>> datetime.strptime(d, "%m/%d/%y").date()
ValueError: time data '31/07/22' does not match format '%m/%d/%y'

As you can see from the above output the ValueError detail helps to show that the format doesn’t align well with the input, therefore look to change the format_string in the second parameter of the strptime function.

Summary

In this article I explained how to use the strptime() function in the datetime library to convert a string in the format of DD/MM/YY to a date object in Python. The function takes two parameters, the first being the string to be converted and the second being the format of the string. The date object output can then be used to perform calculations on now that the string has been successfully formatted.

If you do experience any ValueErrors using the function check both the value of the string being converted that it is a valid date and the format of the string that it matches the composition of the date inserted into the first parameter.

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.