Skip to content

Instantly share code, notes, and snippets.

@Cheslab
Created April 10, 2018 19:45
Show Gist options
  • Save Cheslab/ce0dfbfc97fa3141bde8aad95042d717 to your computer and use it in GitHub Desktop.
Save Cheslab/ce0dfbfc97fa3141bde8aad95042d717 to your computer and use it in GitHub Desktop.
// jQuery UI is required
<form action="" class="form-filters">
<label for="from">From</label>
<input type="text" id="from" name="from">
<label for="to">to</label>
<input type="text" id="to" name="to">
<script>
$(function() {
var dateFrom = new Date();
var dateFormat = "dd.mm.yyyy",
from = $("#from")
.datepicker({
changeMonth: true,
maxDate: dateFrom
})
.on("change", function() {
to.datepicker("option", "minDate", getDate(this));
var currentValueArray = $("#from").val().split(".").reverse();
var currentValueString = '';
currentValueArray.forEach(function(item) {
currentValueString = currentValueString + item + '-';
});
var currentValue = currentValueString.substring(0, 10);
to.datepicker('option', 'maxDate', new Date(currentValue));
}),
to = $("#to").datepicker({
changeMonth: true,
maxDate: dateFrom
})
.on("change", function() {
from.datepicker("option", "maxDate", getDate(this));
});
function getDate(element) {
var date;
try {
date = $.datepicker.parseDate(dateFormat, element.value);
} catch (error) {
date = null;
}
return date;
}
});
</script>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment