• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Script Everything

  • Spreadsheets
  • Python
  • About

Python

Python is a great language to get started with and one that you can operational with quite quickly, especially if you have to deal with data in your line of work.

I have been able to use Python to help wrangle through CSV files, fetch data remotely from the web using the famous requests library, and do some database operations.

Python has a REPL feature that can help you tinker with the code and quickly test the syntax as you learn. There are many helpful resources, and some of the posts below are things I've learned (and am still learning!) along the way, hopefully they can help assist you in your journey too.

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

August 26, 2022 by Ryan Sheehy

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.

Filed Under: strings

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 8
  • Go to page 9
  • Go to page 10
  • Go to page 11
  • Go to page 12
  • Interim pages omitted …
  • Go to page 92
  • Go to Next Page »

Primary Sidebar

Hello! My name is Ryan Sheehy the author of ScriptEverything.com

This website acts as a second brain of sorts for all the hacks and explorations I find when trying to solve problems at work.

About Me

Footer

Spreadsheets

I have been using spreadsheets since as early as 1996 and I continue to use this great productivity tool on a daily basis.

Check out some of my most recent discoveries about spreadsheets.

Python Code

I have been using Python since the late 1990’s due to the limitations of Excel’s VBA. I enjoy being able to wrangle and fetch data externally using Python.

Discover more of what I’ve found with Python.

Apps

Sometimes I play, hack and tinker with other applications to scratch an itch.

To find out the latest hack check out my blog feed.

Copyright © 2023 ScriptEverything.com

  • Contact
  • Privacy Policy