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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.