Reputation:
I'm using tkcalendar to get dates. The date format is 'DD/MM/YYYY'
. However, SQL DATE format accepts only 'YYYY-MM-DD'
.
I have the following code:
dated = cal.get_date()
dates = dated.split('/')
reverse_dates = dates[::-1]
separator = '-'
final_date = separator.join(reverse_dates)
print(final_date)
where, cal
is a tkcalendar.Calendar
object. Is there a shorter way to write this code?
Upvotes: 1
Views: 45
Reputation: 522301
Try using strftime
:
date = cal.get_date()
date_str = date.strftime("%Y-%m-%d")
print("date formatted: ", date_str)
Using a value of datetime.now()
for the starting date, the output from the above script is:
date formatted: 2020-11-23
Upvotes: 2