I am coding a program that sends scheduled emails, but I require the time in code, like this:
send_time = dt.datetime(2021,12,23,4,30,0)
I would like to get this send time as an input from user like:
a = int(input("enter the time:"))
send_time = dt.datetime(a)
Sounds like a good candidate for datetime.strptime
The official documentation can be a bit overwhelming. Here’s a link to a tutorial that explains clearly how to use the strptime
method.
How strptime() works?
The strptime() class method takes two arguments:
string (that be converted to datetime) format code Based on the string and format code used, the method > returns its equivalent datetime object.
In your case, if You could try this:
a = int(input("enter date and time in dd/mm/yyyy H:M:S:"))
send_time = datetime.strptime(a, "%d/%m/%Y %H:%M:%S")
One way to assume today’s date and only add the time:
today = datetime.date.today()
a = input("enter the time HH:MM:SS: ")
# convert input to datetime and replace with today’s year, month and day
timetoday = datetime.datetime.strptime(a, "%H:%M:%S").replace(year=today.year, month=today.month, day=today.day)