Search code examples
jquerytwitter-bootstrapdatetimebootstrap-datetimepicker

Can't get date object from Bootstrap DateTimePicker via JQuery


I can't get the date object after selecting the datetime from the datetimepicker box. How can I get the datetime object and transform it into unix integer?

Here's my html code:

<div class="form-group" align="center">
<div class='input-group date' id="startDate">
    <input type='text' class="form-control" name="org_startDate" placeholder="Wo" data-format="yyyy-MM-dd hh:mm:ss"/>
    <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
    </span>
</div>

Here's my css:

<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

And here my js:

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
jQuery(function () {
    jQuery('#startDate').datetimepicker({
        locale: 'de',
        format: 'dd, DD.MM.YYYY, HH:mm',
        minDate: moment(),
    });
});


// WHAT CODE DO I NEED HERE TO GET DATE OBJECT AND TRANSFORM IT INTO UNIX INTEGER
var dateStr = $("#startDate").find("input").val();
var date = Date.parse(dateStr);

alert(date + " " + new Date(date));

Solution

  • You can access the date value like this:

    var date = $('#startDate').data('DateTimePicker').date();
    

    The date time picker documentation states:

    Note All functions are accessed via the data attribute e.g. $('#datetimepicker').data("DateTimePicker").FUNCTION()