16e
16e

Reputation: 11

how to calcualate how many hours between now and a time

i'm trying to make a timer that can shows how many hours and minutes between now and a specify time and display it with tkinter,i want the timer update every seconds,i'm new to python i dont really know how to do it

import datetime
from datetime import datetime, timedelta
import tkinter as tk
from tkinter import Label
now = datetime.now
time = "09:00:00"
FMT = "%H:%M:%S"
timedelta = datetime.strptime(now, FMT) - datetime.strptime(time, FMT)
print(timedelta)
main = tk.Tk()
main.title("timer")
lb=Label(text=timedelta)
lb.pack()
main.mainloop()

i make alot of mistake on this script as well

Upvotes: 1

Views: 656

Answers (2)

user13801689
user13801689

Reputation:

So I find another answer. Here is the link - How to make a timer program in Python
And here is the code:

import time
# Ask to Begin
#start = input("Would you like to begin Timing? (y/n): ")
#if start == "y":
timeLoop = True

# Variables to keep track and display
Sec = 0
Min = 0
# Begin Process
#timeLoop = start
while timeLoop:
    Sec += 1
    print(str(Min) + " Mins " + str(Sec) + " Sec ")
    time.sleep(1)
    if Sec == 60:
        Sec = 0
        Min += 1
        print(str(Min) + " Minute")

This is like a timer. It counts every second as you need

Upvotes: 0

user13801689
user13801689

Reputation:

I think this might help - How to calculate number of days between two given dates? or this - How do I find the time difference between two datetime objects in python? or maybe this - Measuring elapsed time with the Time module
I tried, and made this:

import datetime
from datetime import date

today = datetime.date.today()
yourday = datetime.date(2020, 7, 15)
timebeetwen = today - yourday
print(timebeetwen.days*24) #hours
print(timebeetwen.days*24*60) #minutes
print(timebeetwen.days*24*60*60) #seconds

Datetime works only with days so the output will be like this:

>>> 48
>>> 2880
>>> 172800

And if you want to show every second use

while True:
    print(timebeetwen.days*24) #hours
    print(timebeetwen.days*24*60) #minutes
    print(timebeetwen.days*24*60*60) #seconds

Upvotes: 2

Related Questions