简体   繁体   中英

How to format date in html from month dd,yyyy to yyyy-mm-dd

I am trying to set value to the input of type 'date', i am fetching data from mysql table and set it as the value attribute of input tag. On first time it doesn't work, but if set date once then it starts working ie input tag shows the content of value attribute even after refreshing.

For first time the date is shown as: Nov 12,2021 and from second time it shows like: 12-11-2021

Here is my code for view:

def profile(request,id):
       project=Project.objects.get(id=id)
       if request.method=='POST':
             deadline=request.post.get('deadline')
             project.deadline=deadline
             project.save()
       return render(request,'edit_project.html',{'id':id,'project':project})

 <html> <body> <form method="POST"> <input type='date' value={{project.deadline}}> <input type='submit' value='submit'> </form> </body> </html>

You need to set DATE_FORMAT = 'Ym-d' in your settings.py. See the docshere .

EDIT: You can also use the template date filter as so:

{{project.deadline|date:"Y-m-d"}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM