2016-12-14 47 views
-2

我工作登记表上在Django 我正在从日期字段从前端输入"12/15/2016" ,但我想它像12 dec 2016保存在我的Django的数据库。 我用这压延日期formator在Django

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
<script> 
    $(document).ready(function() { 
    $("#datepicker").datepicker(); 
    }); 
    </script> 

model.py日期是

posting_date = models.DateField(blank=True) 

Views.py

post_date = str(request.POST['date']) 

date = post_date.split('/') 

formatted_date= date[1]+"-"+date[0]+"-"+date[2] 

我要救十二月,而不是12个月

+0

可能的重复[在django中以所需格式保存日期](http://stackoverflow.com/questions/41140317/save-date-in-django-in-desired-format) –

回答

0

像这样:

from dateutil import parser 
formatted_date = parser.parse("12/15/2016") 
yourobject.posting_data = formatted_date