Is there a way to set maxDate or minDate with a function in bootstrap datapicker? -
i looking simple me did not find in here , in bootstrap datetimepicker documentations well.
the idea simple. have 2 datetime fields have values @ page_load retrieved server.
now want make sure maxdate of first date second date value , mindate of second date date of first field.
here code:
$(document).ready(function () { $('[id^="datetimepickerstartdate"]').datetimepicker({ usecurrent: false, format: applicationdatetimeformat, ignorereadonly: true, //maxdate: function() { // var id = $(this).attr('id'); // var str = id.split("_"); // if (str.length <= 1) { // var seconddatetimepicker1 = $('#datetimepickerfinishdate').data("datetimepicker").date(); // if (seconddatetimepicker1.length) { // return seconddatetimepicker1; // } // return null; // } else { // var seconddatetimepicker2 = $("#datetimepickerfinishdate_" + str[1]).data("datetimepicker").date(); // if (seconddatetimepicker2.length) { // return seconddatetimepicker2; // } // return null; // } //}, locale: locale }); $('[id^="datetimepickerfinishdate"]').datetimepicker({ usecurrent: false, format: applicationdatetimeformat, ignorereadonly: true, //mindate: function () { // var id = $(this).attr('id'); // var str = id.split("_"); // if (str.length <= 1) { // var seconddatetimepicker1 = $('#datetimepickerstartdate').data("datetimepicker").date(); // if (seconddatetimepicker1.length) { // return seconddatetimepicker1; // } // return null; // } else { // var seconddatetimepicker2 = $("#datetimepickerstartdate_" + str[1]).data("datetimepicker").date(); // if (seconddatetimepicker2.length) { // return seconddatetimepicker2; // } // return null; // } //}, //mindate: new date().sethours(0, 0, 0, 0), locale: locale }); $('[id^="datetimepickerstartdate"]') .on("dp.change", function (e) { var id = $(this).attr('id'); var str = id.split("_"); if (str.length <= 1) { $('#datetimepickerfinishdate').data("datetimepicker").mindate(e.date); } else { $("#datetimepickerfinishdate_" + str[1]).data("datetimepicker").mindate(e.date); $(id).datetimepicker('hide'); } }); $('[id^="datetimepickerfinishdate"]') .on("dp.change", function (e) { var id = $(this).attr('id'); var str = id.split("_"); if (str.length <= 1) { $('#datetimepickerstartdate').data("datetimepicker").maxdate(e.date); } else { $("#datetimepickerstartdate_" + str[1]).data("datetimepicker").maxdate(e.date); $(id).datetimepicker('hide'); } $(id).datetimepicker('hide'); }); });
its lot of code looking way how can set rules @ first open of datetimepicker. so, thought execute e function on property maxdate , mindate seemed not work!
any idea how solve issue ?
the dp.change
event works when the date changed first time...
you over-complexing this, looking basically: 1) maxdate first picker 1 selected on second picker. 2) mindate selected on second picker 1 has first picker.
so, there example on official page as: http://eonasdan.github.io/bootstrap-datetimepicker/#linked-pickers
but have values on load of page, have general script:
function aplicarlinkeddatepicker() { var $min = $('.datetimepickermin'); var $max = $('.datetimepickermax'); var valinputmax = $max.children(':input').val(); var valinputmin = $min.children(':input').val(); var format = 'dd/mm/yyyy'; $min.datetimepicker({ format: format, usecurrent: false, }); $max.datetimepicker({ format: format, usecurrent: false, }); $min.on("dp.change", function (e) { $max.data("datetimepicker").mindate(e.date); }); $max.on("dp.change", function (e) { $min.data("datetimepicker").maxdate(e.date); }); if (valinputmin != null) { $max.data("datetimepicker").mindate(moment(valinputmin, format).startof('day')); } if (valinputmax != null) { $min.data("datetimepicker").maxdate(moment(valinputmax, format).endof('day')); } }
so above do? checking if has value input related picker , set respective mindate , maxdate, , if not ignored , let default.
Comments
Post a Comment