Python Forum
convert strings of date to datetime exported from CSV - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: convert strings of date to datetime exported from CSV (/thread-22136.html)



convert strings of date to datetime exported from CSV - GiorgosPap31 - Oct-31-2019

Hello.
I am using python3 to export some date from a csv file.
I have a csv file full of dates in the form of dd/mm/yyyy
Suppose that I have two strings s1='31/10/2019' and s2='20/09/2019' that I got from the csv file
how can I convert those two strings to datetime objects so that I can subtract them and get the days pass between those two dates
Thank you!


RE: convert strings of date to datetime exported from CSV - buran - Oct-31-2019

you can use datetime module from standard library
or install and use some of the popular third-party packages that supposedly make it more convenient to work with dates. If it just for subtracting two dates built-in module should do

>>> from datetime import datetime
>>> s1='31/10/2019'
>>> s2='20/09/2019'
>>> d1 = datetime.strptime(s1, '%d/%m/%Y')
>>> d2 = datetime.strptime(s2, '%d/%m/%Y')
>>> date_diff = (d1 - d2).days
>>> date_diff
41