I need an input prompt that will allow the user to enter a date at the point the function is called and then run to concert the date to Unix date for an API call later in the program.
At the moment every attempt seems to hand back an error regarding Str, to Int.
I get the function wants to convert the date in line 9 to a date time value but I can’t create an input that I could then convert.
def unixconvert() : # importing datetime module import datetime import time # assigned regular string date date_time = datetime.datetime(2021, 7, 26, 21, 20) # print regular python date&time print("date_time =>",date_time) # displaying unix timestamp after conversion print("unix_timestamp => ", (time.mktime(date_time.timetuple())))
I have tried a standard input(‘enter a date’)
I have tried passing a multiple variables and trying to build the input, year =, month = etc.
What about something like this:
def unixconvert(): date_time_input = input() #something like 2021-7-26-21-20 date_time = datetime.datetime(*(int(x) for x in date_time_input.split('-'))) print("date_time =>",date_time) print("unix_timestamp => ", (time.mktime(date_time.timetuple())))
It’s basically split by
- and convert every number to
Maybe something like this is more clear:
year, month, day, hours, minutes = (int(x) for x in date_time_input.split('-')) date_time = datetime.datetime(year, month, day, hours, minutes)