Search code examples
pythonpython-3.xpython-datetime

How do i change date format?


I am new to python. I am trying to change the date format. I have date format like this "YYYY/mm/dd", I want to change this format to "mm/dd/YYYY" this format. How to do this.

views.py

database = Person.objects.all().filter(name= name).values()
for data in database:
    name = data['name']
    age = data['age']
    dob = data['date_of_birth']
    print(name)
    print(age)
    print(dob)

Solution

  • Use datetime methods.

    from datetime import datetime
    a = '2016/02/28'
    date = datetime.strptime(a, '%Y/%m/%d').strftime('%m/%d/%Y')