-2

i working on a registration form in django I taking input from date field from front end is "12/15/2016" but i want to save it like 12 dec 2016 in my django database. I am using this calender

<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>

my model.py for date is

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]

I want to save Dec instead of 12 for month

Zagorodniy Olexiy
  • 2,132
  • 3
  • 22
  • 47
vikrant Verma
  • 478
  • 3
  • 8
  • 17

3 Answers3

0

Do it like this:

from dateutil import parser
formatted_date = parser.parse("12/15/2016")
yourobject.posting_data = formatted_date
Adilet Maratov
  • 1,312
  • 2
  • 14
  • 24
0

Try formatting it using strptime and strftime instead:

formatted_datetime = datetime.strptime(post_date, strftime('%m/%d/%Y')) #formatted_datetime is now a datetime object
print( formatted_datetime.strftime('%d-%b-%Y') )

Documentation:
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

SarTheFirst
  • 350
  • 1
  • 11
0

You can use DATE_FORMAT. See also: date

Dimitar Hristov
  • 123
  • 1
  • 7