Home > Software engineering >  How To Create A Procedure That Given Two Dates And Finds How Many Hours Between Them
How To Create A Procedure That Given Two Dates And Finds How Many Hours Between Them

Time:12-26

So, as I mentioned in the title I should write a code like that. But I can't manage to understand how to do it. Here is my code:

    # Python3 program two find number of
# days between two given dates
 
# A date has day 'd', month
# 'm' and year 'y'
 
 
class Date:
    def init(self, d, m, y):
        self.d = d
        self.m = m
        self.y = y
 
 
# To store number of days in
# all months from January to Dec.
monthDays = [31, 28, 31, 30, 31, 30,
             31, 31, 30, 31, 30, 31]
 
# This function counts the number of
# leap years before the given date
 
 
def countLeapYears(d):
 
    years = d.y
 
    # Check if the current year needs
    # to be considered for the count
    # of leap years or not
    if (d.m <= 2):
        years -= 1
 
    # An year is a leap year if it is a
    # multiple of 4, multiple of 400 and
    # not a multiple of 100.
    ans = int(years / 4)
    ans -= int(years / 100)
    ans  = int(years / 400)
    return ans
 
# This function returns number of
# days between two given dates
 
 
def getDifference(dt1, dt2):
 
    # COUNT TOTAL NUMBER OF DAYS
    # BEFORE FIRST DATE 'dt1'
 
    # initialize count using years and day
    n1 = dt1.y * 365   dt1.d
 
    # Add days for months in given date
    for i in range(0, dt1.m - 1):
        n1  = monthDays[i]
 
    # Since every leap year is of 366 days,
    # Add a day for every leap year
    n1  = countLeapYears(dt1)
 
    # SIMILARLY, COUNT TOTAL NUMBER
    # OF DAYS BEFORE 'dt2'
    n2 = dt2.y * 365   dt2.d
    for i in range(0, dt2.m - 1):
        n2  = monthDays[i]
    n2  = countLeapYears(dt2)
 
    # return difference between
    # two counts
    return (n2 - n1)
 
 
# Driver Code
dt1 = Date(1, 9, 2014)
dt2 = Date(3, 9, 2020)
 
# Function call
print("Difference between two dates is",
      getDifference(dt1, dt2))

But that's not quite working for that question. Am I following the wrong path? Here is what I need to do:

"You are asked to create a procedure that, given two dates, finds how many hours between these dates. This procedure should take 8 parameters as (day1, month1, year1, hour1, day2, month2, year2, hour2). "hour1" and "hour2" parameters must take integer values from 0 to 23. For example:

- Given (1, 12, 2021, 0, 4, 12, 2021, 23) it should output "There are 95 hours.".
- Given (1, 12, 1990, 0, 1, 12, 2021, 18) it should output "There are 271770 hours.".

Could anyone help me with it?

I can't write a code for the time calculation. And I can't use datetime

CodePudding user response:

You're on the right way, your Date-Object constructor should be named __init__ and not init

class Date:
    def __init__(self, d, m, y):
        ...

then your function already seems to return the corrent number of days, so you just need to add the hours into it.

  • Related