Search code examples
jquerypythondjangodatetimepicker

Jquery Datetimepicker to Python Datetime


I have below jQuery Datetimepicker

<input type="text" name="datetime" id="datetime">
$("#datetime").datetimepicker({
    controlType: 'select',
    oneLine: true,
    timeFormat: 'hh:mm tt'
});

I'm sending the date as AJAX POST request to Django:

const starts = $('#datetime').val();
$.ajax({
    type: 'POST',
    url: '/booking/new/',
    data: {
        starts: starts,
    },
};

In Django i get the value:

if request.method == 'POST':
    starts = request.POST.get('starts')
    object.starts = datetime.strptime(starts, '%x %I:%M %p')

I get the following error:

ValueError: time data '11/12/2016 12:00 am' does not match format '%x %I:%M %p'

I tried different formats but I can't find the correct one. Can i produce the format from the string value some how?


Solution

  • You can define default datetime format like this follow some rule