ActiveState Code

Recipe 115421: Date difference


Finds the number of days between two dates.

Python
 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()

Discussion

Someone asked me how to do this.

Comments

  1. 1. At 10:46 a.m. on 22 feb 2002, Hamish Lawson said:

    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?

  2. 2. At 11:27 p.m. on 22 feb 2002, Nelson Rush (the author) said:

    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.

  3. 3. At 7:34 p.m. on 11 mar 2002, Mont McAdam said:

    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.

  4. 4. At 9:10 p.m. on 8 aug 2002, Alex Bartolotti said:

    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.....

Sign in to comment