pythontkintertkcalendar

How to rearrange set date in tkinter


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.


Solution

  • 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")