从datetimepicker jQuery获取minDate,maxDate和defaultDate

时间:2017-09-29 05:07:34

标签: jquery

我构建了datetimepicker,它已经将minDate,maxDate和defaultDate设置为如下图所示。这是取件日期的工作。我想从函数onChangeOpeningDateEdit()上的datetimepicker获取minDate,maxDate和defaultDate,但我不知道如何操作。你能建议我吗?

enter image description here 代码:

<script type="text/javascript">
    $('#date_opening_edit').datetimepicker({
        format: "DD-MMM-YYYY",
        minDate: getMinDateFunction(),
        maxDate: getMaxDateFunction(),
        defaultDate: null,
    });

    function getMaxDateFunction() {
        var d = new Date();
        var y = d.getFullYear();
        var m = d.getMonth();
        var da = d.getDate() + 10;
        var h = d.getHours();
        var mi = d.getMinutes();
        var se = d.getSeconds();
        var mDate = new Date(y, m, da, h, mi, se);
        return mDate;
    };

    function getMinDateFunction() {
        var d = new Date();
        var y = d.getFullYear();
        var m = d.getMonth();
        var da = d.getDate() - 10;
        var h = d.getHours();
        var mi = d.getMinutes();
        var se = d.getSeconds();
        var mDate = new Date(y, m, da, h, mi, se);
        return mDate;
    };

    function onChangeOpeningDateEdit() {
        var minDate = $("#date_opening_edit").datetimepicker("minDate"); // I want to get minDate from datetimepicker, but I don't know how to.
        alert(minDate);
    }
</script>

<div class='input-group date ' id='date_opening_edit'>
    <input type='text' id="txt_opening_date_edit" class="form-control" data-format="DD-MMM-YYYY"
        placeholder="Opening Date" runat="server" onblur="onChangeOpeningDateEdit()" />
    <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
    </span>
</div>

1 个答案:

答案 0 :(得分:1)

试试这个

可以通过数据属性

访问Datepicker选项
function onChangeOpeningDateEdit() {
    var d = planned_element //$("#date_opening_edit")
    var default_date = d.data('DateTimePicker').defaultDate()
    var min_date = d.data('DateTimePicker').minDate()
    var max_date = d.data('DateTimePicker').maxDate()
    alert("minDate:" + min_date +" maxDate:" + max_date + " defaultDate:"+ default_date);
}