Python program to convert hours into days

Here, we will see a Python program to convert time format, converting HH:MM:SS format to number of days. Submitted by Shivang Yadav, on February 18, 2021

Problem statement

We will take time input for two objects from users as hours, minutes and seconds. Then convert this into days, hours, minutes and seconds after adding the values of two objects.

Class and its members used

  • Class : time
    • Method : getTime() : takes input for hours, minutes and second from the user.
    • Method : putResult() : converts that time into required format and prints it.
    • Method : __add__() [addition operator ] : adds the respective values for both objects.
    • Variable : d : store the count of days
    • Variable : h : store the count of hours
    • Variable : m : store the count of minutes
    • Variable : s : store the count of seconds

Program to convert hours into days in Python

class Time:
    def GetTime(self):
        self.__h=int(input("Enter  Hrs: "))
        self.__m = int(input("Enter  Min: "))
        self.__s= int(input("Enter  Second: "))

    def  PutResult(self):
        print("{} Days {} Hours {} Minutes {} Seconds".format(self.__d,self.__h, self.__m, self.__s))
        
    def __add__(self, T):
        R=Time()
        R.__h=self.__h+T.__h
        R.__m=self.__m+T.__m
        R.__s=self.__s+T.__s

        R.__m=R.__m+(R.__s//60)
        R.__s=R.__s%60

        R.__h=R.__h+(R.__m//60)
        R.__m=R.__m%60

        R.__d=R.__h//24
        R.__h=R.__h%24

        return R
        
T1=Time()
T2=Time()

print("Enter value for Time object 1 : ")
T1.GetTime()

print("Enter value for Time object 2 : ")
T2.GetTime()

print("The total time elapsed is ", end = " ")
T3=T1+T2
T3.PutResult()

Output

The output of the above program is:

Enter value for Time object 1 : 
Enter  Hrs: 54
Enter  Min: 32
Enter  Second: 4
Enter value for Time object 2 : 
Enter  Hrs: 79
Enter  Min: 32
Enter  Second: 5
The total time elapsed is  5 Days 14 Hours 4 Minutes 9 Seconds

Python class & object programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.