'Add hours from input , Datetime/Timedelta (Python)
I'm trying to make a script to go to a website and enter in a time in a 24 hour format. I'm using the pyautogui module to perform the web tasks, and Datetime/Timedelta for the time.
I have a big problem, and it's really bugging me.
I want to add the number that the user inputs to the current time, the user input will be in hours. E.G: user enters a 5, the script will add 5 hours to the current time
.
Here's my code:
import pyautogui
import datetime
from datetime import timedelta
xtime = input("enter a number to add in hours: ")
pyautogui.moveTo(833, 566, 1)
pyautogui.click()
datetime.format = '%HH:%MM'
now + timedelta(hours=xtime)
new_time = now + timedelta(xtime)
pyautogui.typewrite(new_time)
pyautogui.press('enter')
Please, if someone knows how I could do this, let me know. I've spent days trying this and nothing works for me. Thanks.
Solution 1:[1]
First of all, if you want to automate a task on a website, a module like pyautogui that controls your mouse or keyboard is not a good solution. Instead, I recommend you to use a module that takes control of your browser like selenium
Then, to convert an integer into hours and add it to an existing time, you can do like this
import time
now = time.time()
add = int(input("The number of hours you want to add : ")) * 3600
format_24h = time.strftime("%HH:%MM", time.localtime(now + add))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | crazycat256 |