# Write a program to find the difference between two times - Python

Categories:
Viewed: 4 - Published at: a few seconds ago

### Write a program to find the difference between two times

``````# function to obtain the time in minutes form
def difference(h1, m1, h2, m2):

# convert h1 : m1 into minutes
t1 = h1 * 60 + m1

# convert h2 : m2 into minutes
t2 = h2 * 60 + m2

if (t1 == t2):
print("Both are same times")
return
else:

# calculating the difference
diff = t2-t1

# calculating hours from difference
h = (int(diff / 60)) % 24

# calculating minutes from difference
m = diff % 60

print(h, ":", m)

# Driver's code
if __name__ == "__main__":

difference(7, 20, 9, 45)
difference(15, 23, 18, 54)
difference(16, 20, 16, 20)
``````

### Write program to find yesterday, today and tomorrow

``````# Import datetime and timedelta
# class from datetime module
from datetime import datetime, timedelta

# Get today's date
presentday = datetime.now() # or presentday = datetime.today()

# Get Yesterday
yesterday = presentday - timedelta(1)

# Get Tomorrow
tomorrow = presentday + timedelta(1)

# strftime() is to format date according to
# the need by converting them to string
print("Yesterday = ", yesterday.strftime('%d-%m-%Y'))
print("Today = ", presentday.strftime('%d-%m-%Y'))
print("Tomorrow = ", tomorrow.strftime('%d-%m-%Y'))
``````

### Write a program to remove all the characters except numbers and alphabets

``````import re

# initialising string
ini_string = "123abcjw:, .@! eiw"

# printing initial string
print ("initial string : ", ini_string)

result = re.sub('[\W_]+', '', ini_string)

# printing final string
print ("final string", result)
``````

### Write a program to merge dict using update() method

``````def Merge(dict1, dict2):
return(dict2.update(dict1))

# Driver code
dict1 = {'a': 10, 'b': 8}
dict2 = {'d': 6, 'c': 4}

print(Merge(dict1, dict2))
print(dict2)
``````

### Write a program to print even length words in a string

``````def printWords(s):
s = s.split(' ')
for word in s:
if len(word)%2==0:
print(word)
# Driver Code
s = "hello world"
printWords(s)
``````

### Write a program to delete all duplicate letters in a string

``````def removeDuplicate(str):
s=set(str)
s="".join(s)
print("Without Order:",s)
t=""
for i in str:
if(i in t):
pass
else:
t=t+i
print("With Order:",t)

str="helloworld"
removeDuplicate(str)
``````