Set Today date to kendo datepicker

不想你离开。 提交于 2020-01-09 10:15:10

问题


I want to set today date to Kendo DatePicker on clear button click. I tried following but it is not working.

$('#btnClear').click(function () {
  $("#StartDate").data("kendoDatePicker").value(new Date());
});

Above code don't give any error and don't set today date. It clears kendo DatePicker's textbox value. Note: Kendo DatePicker format is MM/dd/yyyy.


回答1:


I tried following and works perfectly for me.

$('#btnClear').click(function () {
  var todayDate = kendo.toString(kendo.parseDate(new Date()), 'MM/dd/yyyy');
  $("#StartDate").data("kendoDatePicker").value(todayDate);
});



回答2:


 $('#btnClear').click(function (e) {
  var todayDate = new Date();
  $('#StartDate').data("kendoDatePicker").value(todayDate);
                                  });



回答3:


After setting the value of the datepicker, you need to trigger the change event of the datePicker e.g:

$("#StartDate").data("kendoDatePicker").trigger("change");

Explanation from Telerik:

"The DatePicker will not apply the "new" date if it is the same as its internal value. When you call the date in the method [they mean by using datepicker.value(myDate)] and just set its date, then the internal date of the DatePicker is set too"

See also http://www.telerik.com/forums/datepicker-does-not-update-the-value-in-view




回答4:


I have use it like -

 @(Html.Kendo().DatePicker()
                  .Name("customerOrderDate")
                  .Min(DateTime.Today)
                  .Value(Model.CustomerOrderDate)
                  .HtmlAttributes(new {style = "width:120px"}))

It is good part that Kendo have DateTime struct in their api.




回答5:


The answer didn't work for me it wasn't until I triggered a change event before it set.

var datePicker = $("#StartDate").data("kendoDatePicker");
var todayDate = new Date();                                   
datePicker.value(todayDate);
datePicker.trigger("change"); // <-- This one did the trick



回答6:


Please See this Example May Be helpful to you

http://rniemeyer.github.io/knockout-kendo/web/DatePicker.html



来源:https://stackoverflow.com/questions/25337509/set-today-date-to-kendo-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!