How to rearrange set date in tkinter

475 Views Asked by At

This is my code

e1 = DateEntry(frameLabDate, textvariable=cVarChange, date_pattern='dd/mm/yyyy')
e1.grid(row=0, column=2, padx=8)

I'm calling the date value inside a function

def fun():
    a=e1.get()
    Date = datetime.strptime(a, '%Y/%m/%d')

So here my input is like 22/10/2020. My desired output is 2020/10/22

After executing the above code I get the following error.

ValueError: time data '2020-10-22' does not match format '%Y/%m/%d'

Thanks in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

You can try:

from datetime import datetime

def fun(a):
    date_obj = datetime.strptime(a, '%d/%m/%Y')
    date_str = datetime.strftime(date_obj, '%Y/%m/%d')
    return date_str

fun("22/10/2020")
3
On

You should use get_date() instead of get() and use strftime() to convert the date to your desired format:

def fun():
    a = e1.get_date() # return datetime.date object
    print(a.strftime('%Y/%m/%d')) # convert to desired format