Welcome, guest | Sign In | My Account | Store | Cart

Finds the number of days between two dates.

Python, 84 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# cal.py
#
# This code has been released to the Public Domain.
#
# finds the number of days between two particular dates
#
from string import *

FALSE,TRUE = range(2)

# Standard number of days for each month.
months = (31,28,31,30,31,30,31,31,30,31,30,31)

JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC = range(len(months))

def leapyear(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return TRUE
            else:
                return FALSE
        else:
            return TRUE
    else:
        return FALSE

def main():
    days=sum=0
    month = atoi(raw_input("Enter Month 1: "))
    day = atoi(raw_input("Enter Day 1: "))
    year = atoi(raw_input("Enter Year 1: "))
    emonth = atoi(raw_input("Enter Month 2: "))
    eday = atoi(raw_input("Enter Day 2: "))
    eyear = atoi(raw_input("Enter Year 2: "))

    month = month - 1
    emonth = emonth - 1

    if month == JAN:
        if leapyear(year):
            days = days + (366 - day)
        else:
            days = days + (365 - day)
    else:
        i = 0
        while i < month:
            sum = sum + months[i]
            i = i + 1
        sum = sum + day
        if leapyear(year):
            days = days + (366 - sum)
        else:
            days = days + (365 - sum)

    print "Days first year ==",days
    print "Number of years between ==",eyear - year

    i = year + 1
    while i < eyear:
        if leapyear(i):
            days = days + 366
        else:
            days = days + 365
        print "in year",i
        i = i + 1

    print "Total days not including last year ==",days

    if emonth == JAN:
        days = days + eday
    else:
        i = 0
        while i < emonth:
            days = days + months[i]
            i = i + 1
        days = days + day
        if leapyear(year) and emonth > FEB:
            days = days + 1

    print "Final total days ==",days

if __name__ == '__main__':
    main()

Someone asked me how to do this.

5 comments

Hamish Lawson 22 years, 1 month ago  # | flag

mxDateTime? It may be worth noting that the module only handles dates since the adoption of the Gregorian calendar reforms; different states adopted the reforms at different times from the 16th century onwards.

Also, might it be better to employ the date manipulation services provided by the mxDateTime package?

Nelson Rush (author) 22 years, 1 month ago  # | flag

Okay... Well it was a programming exercise. The guy I was helping had to show to his teacher he was capable of actually programming this.

Yes there is plenty of room for improvement.

Mont McAdam 22 years ago  # | flag

Was the objective of the task to re-invent the wheel? Wouldn't it have been easier to; import time and convert the two dates to a unix timestamp then find the difference in seconds and devide the result by 86400 (seconds in a day).

Just a thought.

Alex Bartolotti 21 years, 7 months ago  # | flag

Doesn't work. I have converted the code to a Java program and for two dates that have the same year, it does not work. Hmmm.....

a 13 years, 11 months ago  # | flag

atoi() is deprecated. you should use int() instead

Created by Nelson Rush on Wed, 20 Feb 2002 (PSF)
Python recipes (4591)
Nelson Rush's recipes (8)

Required Modules

Other Information and Tasks