Python Forum
convert strings of date to datetime exported from CSV
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert strings of date to datetime exported from CSV
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!
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 670 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Convert date integers (ex. 43831) to regular format Galven 2 808 Nov-15-2020, 11:38 PM
Last Post: bowlofred
  Convert file of hex strings to binary file medatib531 4 2,329 Oct-09-2020, 05:42 PM
Last Post: DeaD_EyE
  How to add date and years(integer) to get a date NG0824 4 1,011 Sep-03-2020, 02:25 PM
Last Post: NG0824
  API with parameters exported to sql database PYTHONDUDE 0 493 Jul-20-2020, 01:18 PM
  Pass integers to function florian 3 981 Jul-18-2020, 04:43 AM
Last Post: scidam
  Convert calendarweek into date and2handles 1 930 Jun-30-2020, 02:42 PM
Last Post: DeaD_EyE
  Convert weekly sequences to date and time. SinPy 0 609 Nov-23-2019, 05:20 PM
Last Post: SinPy
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 3,583 Sep-18-2019, 08:32 AM
Last Post: buran
  building functions to validate strings , date etc metro17 2 1,051 Aug-08-2019, 12:42 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020