Numworks Python: Days Between Days Without Converting to Julian Dates
Introduction
The script daysbtwn.py find the number days between two days given the month and year. The program is designed to find the dates, not including the last date (to match most financial calculators and spreadsheets), within one calendar year. The program will ask if February 29 is included.
Numworks Python Code: daysbtwn.py
from math import *
# days between dates within a year
# no conversion to julian approach
# store basic calendar
# 0th is 0 to match the months
cal=[0,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31]
# ask for dates
m1=int(input("Month 1? "))
d1=int(input("Day 1? "))
m2=int(input("Month 2? "))
d2=int(input("Day 2? "))
# leap year question
l=0
l=int(input("Include Feb. 29?\nYES=1, NO=0: "))
# sum
if m1==m2:
s=d2-d1
if s<=0:
s+=365+l
else:
if m2<m1:
m2+=12
s=cal[m1]-d1
s+=sum(cal[m1+1:m2])
s+=d2+l
print("DAYS = ",s)
The code has the math module, which can be used with any calculator with Python or the computer version.
Examples
March 14 – March 13 (of next year)
Month 1? 3, Day 1? 14
Month 2? 3, Day 2? 13
Include Feb. 29? 0
Number of Days: 364
March 14 – March 14 (of next year)
Month 1? 3, Day 1? 14
Month 2? 3, Day 2? 14
Include Feb. 29? 0
Number of Days: 365
March 14 – March 15 (of the same year)
Month 1? 3, Day 1? 14
Month 2? 3, Day 2? 15
Include Feb. 29? 0
Number of Days: 1
January 1 – December 31 (of the same year – leap year)
Month 1? 1, Day 1? 1
Month 2? 12, Day 2? 31
Include Feb. 29? 1
Number of Days: 365
In the U.S., wishing you a safe, sane, and Happy Thanksgiving.
Eddie
All original content copyright, © 2011-2025. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.