How can I restructure a string of numbers and convert them to integers?

Question:

I have a list that contains dates as a string:

date_list = [['4272', '07/18/2022'], ['4271', '07/18/2022'], ['4254', '06/23/2022'], ['4222', '05/09/2022'], ['4174', '03/09/2022'], ['3946', '06/07/2021'], ['3918', '05/03/2021'], ['3914', '08/19/2021'], ['3907', '08/19/2021'], ['3888', '07/05/2022'], ['3784', '12/21/2020'], ['3651', '05/07/2020'], ['3644', '04/20/2020'], ['3615', '02/06/2020'], ['3140', '09/24/2018'], ['3125', '03/03/2022']]

The plan is to use datetime() to compare these dates with today’s date and I’ll return the output. Obviously for each item in the list, the first value is a unique ID number and the second value is the date. I’m not sure how to pull out the date from each item and re-format it so it can be used with datetime(). Using the first item in my list as an example, I need it to go from the string '07/18/2022' into the function as valid_date = datetime.date(2022,07,18) as ints, so it can run. I have to iterate through each date in my list and run it through datetime()

today = datetime.date.today()
valid_date = datetime.date(<year>,<month>,<day>)
diff = valid_date - today
Asked By: Mitchell Privett

||

Answers:

You can use the strptime from datetime module. It takes a string and a format then gives you a datetime object:

from datetime import datetime

date_list = [
    ["4272", "07/18/2022"],
    ["4271", "07/18/2022"],
    ["4254", "06/23/2022"],
    ["4222", "05/09/2022"],
    ["4174", "03/09/2022"],
    ["3946", "06/07/2021"],
    ["3918", "05/03/2021"],
    ["3914", "08/19/2021"],
    ["3907", "08/19/2021"],
    ["3888", "07/05/2022"],
    ["3784", "12/21/2020"],
    ["3651", "05/07/2020"],
    ["3644", "04/20/2020"],
    ["3615", "02/06/2020"],
    ["3140", "09/24/2018"],
    ["3125", "03/03/2022"],
]

for num, date_string in date_list:
    datetime_object = datetime.strptime(date_string, "%m/%d/%Y")
    today = datetime.today()
    diff = today - datetime_object
    print(diff)
Answered By: S.B

You can use strptime() to parse the date.

parsed = datetime.strptime("07/18/2022", "%m/%d/%Y")
print(type(parsed))

Expected output:

<class 'datetime.datetime'>
Answered By: Mushroomator

Using strptime would be the pythonic and best solution as suggested by others. But anyway, this might be you asking for:

import datetime

date_list = [['4272', '07/18/2022'], ['4271', '07/18/2022'], ['4254', '06/23/2022'], ['4222', '05/09/2022'], ['4174', '03/09/2022'], ['3946', '06/07/2021'], ['3918', '05/03/2021'], ['3914', '08/19/2021'], ['3907', '08/19/2021'], ['3888', '07/05/2022'], ['3784', '12/21/2020'], ['3651', '05/07/2020'], ['3644', '04/20/2020'], ['3615', '02/06/2020'], ['3140', '09/24/2018'], ['3125', '03/03/2022']]

for ID, date in date_list:
    month, day, year = date.split('/')
    month, day, year = int(month), int(day), int(year)
    today = datetime.date.today()
    valid_date = datetime.date(year, month, day)
    diff =  today - valid_date
    print(diff.days)
Answered By: Arifa Chan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.