-1

If there are two given dates(e.g., from Oct 12, 2016 to Nov 21, 2019). Based on the Act/Act Convention, how can I calculate the amount of days by the "While" or "IF" loop?

  • 2
    Does this answer your question? [How to calculate number of days between two given dates?](https://stackoverflow.com/questions/151199/how-to-calculate-number-of-days-between-two-given-dates) – sahasrara62 Apr 26 '20 at 01:48
  • Can you be more specific about what the issue is? Please see [ask], [help/on-topic]. – AMC Apr 26 '20 at 01:57

2 Answers2

0

You can use datetime module, i.e.:

from datetime import datetime

df = "%b %d, %Y"
start = datetime.strptime('Oct 12, 2016', df)
end = datetime.strptime('Nov 21, 2019', df)
diff = end - start
print (diff.days)
# 1135

Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

The fundamental rule is for days fall in a leap year you divide by 366 and for the rest you devide by 365.

Let me use the following example. date1 = datetime(2020, 5, 8) date2 = datetime(2021, 2, 26)

  1. For the starting year 2020, which is a leap year, the REMAINING days should be divided by 366 to reflect ACT/ACT convention. For the ending year 2021, the PASSED days to date should be divided by 365.

So basically it becomes (datetime(2020.12.31) - datetime(2020, 5, 8)).days / 366 + (datetime(2021, 2, 26) - datetime(2021, 1, 1)).days / 365

  1. Expand on above example, assume date2 = datetime(2025, 2, 26). There are more years in between, i.e. [2020, 2021, 2022, 2023, 2024, 2025]. But the middle years should all count as one, and the tricky part sill remains in handling the starting and ending year.