-1

I have to calculate the time between two dates in days in Python, but without the datetime module.

I managed to parse a date without datetime:

def date(error):
    global sdate
    if error:
        print(sdate," isn't a valable date ! ")
    sdate = input('Date : ')
    try:
        ldate = sdate.split('.')
        ldate = [ int(x) for x in ldate]
        day,month,year = ldate
    except ValueError:
        date(True)
    if year%4==0 and year%100!=0 or year%400==0:
        bis = True
    else:
        bis = False
    if month not in range(1,13):
        date(True)
    if month not in(1,3,5,7,8,10,12):
        mmax = 31
    elif month in(4,6,9,11):
        mmax = 30
    elif month == 2 and bis == True:
        mmax = 29
    elif month == 2 and bis == False:
        mmax = 28
    if day not in range(1,mmax+1):
        date(True)
date(None)
print(ldate)

But didn't figure out how to get time between dates.

What's the easiest way?

Thanks, Blaxou

PS: It's not a Homework at all, I need it for a personal project where I use any of too easy-life-making modules ;)

Blax
  • 500
  • 3
  • 7
  • 18

1 Answers1

1

You will have to do much more than that to check the difference between two dates.

I will give you pseudo code to do this, write your own program.

• get the dates
• validate the dates 
    -> (ex. It should be false for feb 29 2007)
• calculate total number of days of that particular year for both dates 
    -> (ex 1 jan 2015 = 01 and 28 feb 2016 = 59)
• calculate the year difference
• calculate number of leap years between two days (excluding both end)
    -> (2004 and 2008 leap year is 0)
• calculate the difference by
  diff(number of days) + (year difference * 365) + number of leap years
Prajwal
  • 3,930
  • 5
  • 24
  • 50