skp says:
Now, date is getting saved into the database, but I just wanted to save only the month name and year. For eg : March 2019.
Replace
data: { MinValue: new Date(ui.values[0]), MaxValue: new Date(ui.values[1]) },
above with below.
data: { MinValue: formatDate(new Date(ui.values[0])), MaxValue: formatDate(new Date(ui.values[1])) },
skp says:
It is also getting saved as multiple records when I just slide from one month to next month.
That i already told you with slide event multiple record will be inserted in database.
So you need to use stop event. It only insert the record after stopping slide.
Refer below code.
<script type="text/javascript">
function formatDate(date) {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = date.getMonth();
var year = date.getFullYear();
return monthNames[monthIndex] + ' ' + year;
}
$(function () {
$("#slider-range").slider({
range: true,
min: new Date('2019-01-01').getTime(),
max: new Date('2019-12-01').getTime(),
step: 86400000,
values: [new Date('2019-03-01').getTime(), new Date('2019-06-01').getTime()],
slide: function (event, ui) {
$("#amount").val(formatDate(new Date(ui.values[0])) + '-' + formatDate(new Date(ui.values[1])));
},
stop: function (event, ui) {
$.ajax({
data: { MinValue: formatDate(new Date(ui.values[0])), MaxValue: formatDate(new Date(ui.values[1])) },
type: "post",
url: apiUrl + "/api/Warehouse/addMinMaxValues",
success: function (data) {
//alert("Data Save: " + data);
}
});
}
});
$("#amount").val(formatDate((new Date($("#slider-range").slider("values", 0))))
+ " - " + formatDate((new Date($("#slider-range").slider("values", 1)))));
});
</script>