# New to Python - Need date calculation help

This project will determine the following

1. Is a loan active in a defined period
2. If a loan is active in a defined period, how many days was it active

Define the Custom Periods

`from datetime import datetime, date, timedelta`

`fy22_p1_start = date(2021, 9, 26)`
`fy22_p1_end = date(2021, 10, 30)`
`fy22_p2_start = date(2021, 10, 31)`
`fy22_p2_end = date(2021, 11, 27)`
`fy22_p3_start = date(2021, 11, 28)`
`fy22_p3_end = date(2021, 12, 25)`
`fy22_p4_start = date(2022, 12, 26)`
`fy22_p4_end = date(2022, 1, 29)`
`fy22_p5_start = date(2022, 1, 30)`
`fy22_p5_end = date(2022, 2, 26)`
`fy22_p6_start = date(2022, 2, 27)`
`fy22_p6_end = date(2022, 3, 26)`
`fy22_p7_start = date(2022, 3, 27)`
`fy22_p7_end = date(2022, 4, 30)`
`fy22_p8_start = date(2022, 5, 1)`
`fy22_p8_end = date(2022, 5, 28)`
`fy22_p9_start = date(2022, 5, 29)`
`fy22_p9_end = date(2022, 6, 25)`
`fy22_p10_start = date(2022, 6, 26)`
`fy22_p10_end = date(2022, 7, 30)`
`fy22_p11_start = date(2022, 7, 31)`
`fy22_p11_end = date(2022, 8, 27)`
`fy22_p12_start = date(2022, 8, 28)`
`fy22_p12_end = date(2022, 9, 24)`
`fy23_p1_start = date(2022, 9, 25)`
`fy23_p2_end = date(2022, 10, 29)`

‘’‘Get input from user about Loan Start and Loan End dates and convert to dates’‘’

`loan_start = str(input('Loan Start date(yyyy-mm-dd): '))`
`loan_start = datetime.strptime(loan_start, '%Y-%m-%d').date()`

`loan_end = str(input('Loan End date(yyyy-mm-dd): '))`
`loan_end = datetime.strptime(loan_end, '%Y-%m-%d').date()`

`print('')`
`print(f'Period Start Date:',fy22_p7_start)`
`print(f'Period End Date:',fy22_p7_end)`
```print(‘’)``````

‘’‘Determine if Loan is in a specific period’‘’

`if loan_start > fy22_p7_end or loan_end < fy22_p7_start:`
` in_period = ('Loan was not in Period')`
` print(in_period)`
`else:`
` in_period = ('Loan was in Period')`
` print(in_period)`

‘’‘Calculate Days in Period start and end’‘’

`if loan_start >= fy22_p7_start:`
` per_calc_start = loan_start`
`else:`
`per_calc_start = fy22_p7_start`

`if loan_end <= fy22_p7_end:`
`per_calc_end = loan_end`
`else:`
`per_calc_end = fy22_p7_end`

‘’‘Calculate Days in Period Days’‘’

`period_days = (per_calc_end - per_calc_start)`

`if period_days > 0:`
`print(f'Your loan was active for', period_days, 'this period')`

``````    print('Your loan was not active in this period')```

'''Calculate total loan days'''

```days = (loan_end - loan_start)
print(f'Total Loans Days:', days)```

Output

Loan Start date(yyyy-mm-dd):  2022-09-01
Loan End date(yyyy-mm-dd):  2022-09-15

Period Start Date: 2022-03-27
Period End Date: 2022-04-30

Loan was not in Period

Error

TypeError                                 Traceback (most recent call last)
Input In , in <cell line: 75>()
71 '''Calculate Days in Period Days'''
73 period_days = (per_calc_end - per_calc_start)
---> 75 if period_days > 0:
76     print(f'Your loan was active for', period_days, 'this period')
77 else:

TypeError: '>' not supported between instances of 'datetime.timedelta' and 'int'

Can't figure out how to create calculation``````

Please put triple backticks (“code fences”) around your code, it
preserves the indenting (and a lot of other stuff) for others to see.
Eg:

`````` ```
```
``````

Fortunately I’m on email and get this stuff raw Your issue is that, generally, you cannot compare objects of different
types in Python (because that will usually be nonsensical). So, your
code has:

`````` '''Calculate Days in Period start and end'''
if loan_start >= fy22_p7_start:
per_calc_start = loan_start
else:
per_calc_start = fy22_p7_start
if loan_end <= fy22_p7_end:
per_calc_end = loan_end
else:
per_calc_end = fy22_p7_end
``````

So `per_calc_start` and `per_calc_end` are `date`s.

Then you do this:

`````` period_days = (per_calc_end - per_calc_start)
``````

so that `period_days` is now a `timedelta`, the result of arithmetic
between `date`s:
https://docs.python.org/3/library/datetime.html#datetime.timedelta

Then you go:

`````` if period_days > 0:
``````

which raises a `TypeError` because `period_days` is a `timedelta` and
`0` is an `int`. What you want is the number of days the `timedelta`
represents. Fortunately there’s a `.days` attribute (see the docs
above), so you could do this:

`````` if period_days.days > 0:
``````

But I’d actually be inclined to put that in the previous calculation:

`````` period_days = (per_calc_end - per_calc_start).days
if period_days > 0:
``````

or to change the variable name:

`````` period = per_calc_end - per_calc_start
if period.days > 0:
``````

just to make the meaning of “period” or “period_days” more clear.

Cheers,
Cameron Simpson cs@cskk.id.au

Cameron,

Sorry about the formatting, its my first post and I have only started learning Python this week. I really appreciate the help. It worked just like you demonstrated adding the .days.

Thank you so much

By Rshypitka via Discussions on Python.org at 12Sep2022 23:17:

Sorry about the formatting, its my first post and I have only started
learning Python this week.

The formatting thing is completely normal, it is not as obvious as it
should be maybe.

Cheers,
Cameron Simpson cs@cskk.id.au