Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Days between two dates kendo DateTimePicker

Good day, I need to get the number of days between two dates entered into two kendo.DateTimePickers.

My solution always ends with 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>
like image 858
Patrik Volka Avatar asked Oct 30 '22 04:10

Patrik Volka


1 Answers

Try using Moment.js to convert the values from the date pickers into date objects. That will solve your problems.

Something like...

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

I actually recommend using Moment.js for pretty much any time you're manipulating dates, especially when those dates come from web page elements. Kendo is pretty good, but I've had issues trying to use the dates directly.

like image 51
Avrohom Yisroel Avatar answered Nov 15 '22 05:11

Avrohom Yisroel