I have a date that is generated from the user side. However this date is in string format Here is an example below:
period = '12-4-2020'
how can I convert it to datetime format?
You can use the datetime.datetime.strptime
method:
period = '12-4-2020'
datetime.datetime.strptime(period, '%m-%d-%Y')
or (the order of day month year is ambiguous based on that date string)
datetime.datetime.strptime(period, '%d-%m-%Y')
The first argument is your date string, and the second argument is the string format.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments