Convert date format python
Question: I have django form and I am receiving from POST a date formated like
"%d/%m/%Y" and I would like to convert it to "%Y-%m-%d", How could I do it?


Answer: Use [strptime and
strftime](http://docs.python.org/library/datetime.html#strftime-strptime-
behavior):

    
    
    In [1]: import datetime
    
    In [2]: datetime.datetime.strptime('10/05/2012', '%d/%m/%Y').strftime('%Y-%m-%d')
    Out[2]: '2012-05-10'
    

Likewise, in Django template syntax you can use the [date
filter](https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date):

    
    
    {{ mydate|date:"Y-m-d" }}
    

to print your date in your preferred format.

