Trying to convert %Y-%m-%d" to "%d-%m-%Y". The object im converting is "unicode" from a date input in a form. I need to send this start date to my SQL query to get data from the table for that time.
I've done this previously but it doesn't work anymore as the start_date to be converted is in the format "%Y-%m-%d". It must end up in %d-%m-%Y in my sql query!
start= datetime.datetime.strptime(start_date, "%Y-%m-%d")
end= datetime.datetime.strptime(end_date, "%Y-%m-%d")
start_time=start.strftime("%d-%m-%Y")
end_time=end.strftime("%d-%m-%Y")
Run Code Online (Sandbox Code Playgroud)
I want to convert the received input form form 2019-06-19 to 19-06-2019. Thank you so much. I don't know why i'm finding it so hard.
您可以datetime.datetime使用其当前格式将时间字符串解析为,然后使用新格式来格式化结果对象:
datetime.datetime.strptime(thing, "%Y-%m-%d").strftime("%d-%m-%Y")
Run Code Online (Sandbox Code Playgroud)
例如:
>>> datetime.datetime.strptime('2019-05-18', "%Y-%m-%d").strftime("%d-%m-%Y")
'18-05-2019'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |