Days Between Two Dates kendo DateTimePicker

Good afternoon, I need to get the number of days between two dates enclosed in two kendo.DateTimePickers.

My decision always ends with a NaN value. RentStartDate and RentEndDate are stored in db as DateTime.

Thank you for your advice.

<script>$("#RentEndDate").change(function () { var startDate = kendo.toString($("#RentStartDate").data("kendoDateTimePicker").value(), "dd.MM.yyyy"); var endDate = kendo.toString($("#RentEndDate").data("kendoDateTimePicker").value(), "dd.MM.yyyy"); alert(calculate(startDate, endDate)); }); function calculate(first, second) { var diff = Math.round((second - first) / 1000 / 60 / 60 / 24); return diff; } 

CreateOrders.cshtml

 <h4>Termín půjčení</h4> <div class="t-col t-col-6 t-col-xs-12 t-col-sm-12 t-col-md-12 col-sm-6"> <label for="rentStartPicker">Půjčit od</label> @(Html.Kendo().DatePickerFor(model => model.RentStartDate).Name("rentStartPicker").HtmlAttributes(new { style = "height:28px;", required = "required", validationmessage = "Vyberte datum" })) </div> <div class="t-col t-col-6 t-col-xs-12 t-col-sm-12 t-col-md-12 col-sm-6"> <label for="rentEndPicker">Půjčit do</label> @(Html.Kendo().DatePickerFor(model => model.RentEndDate).Name("rentEndPicker").HtmlAttributes(new { style = "height:28px;", required = "required", validationmessage = "Vyberte datum" })) </div> 
+5
source share
2 answers

Try using Moment.js to convert values ​​from date pickers to date objects. This will solve your problems.

Sort of...

 var startDate = moment($("#RentStartDate").data("kendoDateTimePicker").value()); 

I really recommend using Moment.js almost any time you manipulate dates, especially when these dates come from web page elements. Kendo is very good, but I had problems trying to use dates directly.

+1
source

Thanks for pointing out ... The problem is resolved, so it looks like a functional script.

 $("#RentEndDate").change(function () { var startDate = $("#RentStartDate").data("kendoDateTimePicker").value(); var endDate = $("#RentEndDate").data("kendoDateTimePicker").value(); var diffDay = calculate(startDate, endDate); alert(diffDay.toString()); }); 
+1
source

Source: https://habr.com/ru/post/1270111/


All Articles