Dhwani
Dhwani

Reputation: 7626

Set Today date to kendo datepicker

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.

Upvotes: 24

Views: 86004

Answers (6)

Demodave
Demodave

Reputation: 6662

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

Upvotes: 3

Chris Halcrow
Chris Halcrow

Reputation: 31990

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

Upvotes: 5

Aamol
Aamol

Reputation: 1199

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.

Upvotes: 3

Rishi Shrivastava
Rishi Shrivastava

Reputation: 301

Please See this Example May Be helpful to you

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

Upvotes: -2

Dhwani
Dhwani

Reputation: 7626

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);
});

Upvotes: 43

GotaloveCode
GotaloveCode

Reputation: 1034

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

Upvotes: 11

Related Questions