Search code examples
bootstrap-datetimepicker

How to send a differently formatted DateTime to server in Bootstrap DateTimePicker than the one shown to user


Suppose you want to show the user a date this way:

2017-03-22 22:53

But you want the form to send 2017-03-22T22:53:00-03:00 to the server.

How can I do this?


Solution

  • You have to set the field's value at submit event.

    var options = {
        'sideBySide': true,
        'showTodayButton': true,
        'useCurrent': true,
        'extraFormats': [moment.ISO_8601]
    };
    var $dpStart = $('#start-date');
    var $dpEnd = $('#end-date');
    $dpStart.datetimepicker(options);
    $dpEnd.datetimepicker(options);
    $('#the-form').on('submit', function(event){
        $dpStart.val($dpStart.data("DateTimePicker").date().format());
        $dpEnd.val($dpEnd.data("DateTimePicker").date().format());
    });