Python 3 Date calculation after reading from JSON file not working

Hi folks,

I have a piece of code that is trying to read out member details from a file and then use a date in that file together with today's date to work out the approximate difference in months (I have not yet started to tackle the exact days in a given month yet) and see if they are over 12 months therefore I can class them as an 'overdue' payment.

I have tried a number of different ways and seem to end up looping around back to TypeError: an integer is required (got type str).

I have attached a cut down version with just the part of the code with an error. I think the issues is within these following lines however I could be wrong:

d = datetime.datetime.now()

lastdatepaid = int(datetime.datetime("%Y-%m-%d"))

newldp = datetime.datetime.strptime(lastdatepaid,"%Y-%m-%d").date()

datechange =(d - lastdatepaid).days

It might help to have the JSON file data:

[[{"DOB:": "2001-11-17", "FeeType:": "SP", "Firstname:": "Joe", "Last Date Paid:": "2016-11-01", "Money Owed: \u00a3": 100, "Months Overdue:": 0, "Months til Overdue:": 12, "Surname:": "Bloggs", "Times Overdue:": 1}]]

N.B. Not all aspects of the project are fully completed but this is my biggest problem I have not been able to solve.

Thank you very much in advance for you help.
Cut-down-version.py
Karl01Asked:
Who is Participating?
 
Flabio GatesConnect With a Mentor Commented:
Guessing from your other code:
datechange =(d - lastdatepaid).days

Open in new window

that you expect lastdatepaid to be a datetime object
then you should change
lastdatepaid = int(datetime.datetime("%Y-%m-%d"))

Open in new window

to
lastdatepaid = datetime.datetime.strptime(x['Last Date Paid:'], '%Y-%m-%d')

Open in new window

where x is a dictionary from your json string
0
 
Flabio GatesCommented:
Your first error is in this line:
lastdatepaid = int(datetime.datetime("%Y-%m-%d"))

Open in new window

Specifically here:
datetime.datetime("%Y-%m-%d")

Open in new window

What do you expect/want to get as the value of lastdatepaid?
0
 
Flabio GatesCommented:
Question inactive
0
 
Karl01Author Commented:
Hi,

Sorry for the long delay - I left work unexpectedly before Christmas and have been off since.

Your solution has solved my problem, thank you very much for your time and expertise. I greatly appreciate it.

Best Regards
Karl
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.