Search code examples
pythondatetime-formatstrftimemktime

Conversion of date 8-DEC-17 to 08/12/2017 is giving 07/12/2017 in python


I'm trying to format the datetime in python, and this is what I was trying:

import time

datestr = "8-DEC-17"
v=time.strptime(datestr,"%d-%b-%y")
l = time.mktime(v) 
print(time.strftime("%d/%m/%y ", time.gmtime(l)))

The output of this code is : 07/12/17 which is not the one I want

I am expecting : 08/12/17


Solution

  • You can use datetime which is a bit shorter and gives the result you want:

    from datetime import datetime 
    datetime.strptime(datestr, "%d-%b-%y").strftime("%d/%m/%y")