← Back to challenges

How Many Days between Two Dates?

PythonHardalgorithmsconditionsdateslogic

Instructions

Your task is to calculate the number of days between two dates. The dates will be in the format DDMMYYYY. You are not allowed to import any modules, especially the datetime module. The days will not include the end date in calculation.

Remember to consider all leap years and leap months. The order of the larger date and smaller date don't matter, as the days between them are the same anyways.

Examples

days_between_dates("01012020", "02012020") ➞ 1

days_between_dates("03101999", "02023000") ➞ 365,365

days_between_dates("03101534", "07013443") ➞ 696,969

days_between_dates("30012020", "01012020") ➞ 29

Notes

  • Take note that a leap year is divisible by 4. However, if it is a new century (like 1900, 2400, etc), check its divisibility by 400. If it doesn't divvy into a whole number, then it is not a leap year (1900 isn't a leap year but 2400 is).
  • Do comment if there are any bugs or problems.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.