Kendo UI datepicker doesn't display value initially when formatted

Mentor picture Mentor · Sep 30, 2013 · Viewed 16.3k times · Source

I have an issue with Kendo UI datepicker, it does not display an already set value through value attribute. Here's the markup:

<input data-kendoDatePicker="true" value="9/18/2013 12:00:00 AM"/>

<script>
    $(':input[data-kendoDatePicker=true]').kendoDatePicker({
        format: 'dd MMM yyyy'
    });
</script>

When the page loads it wont display any value. However, looking in the DOM the value is set for the input, it just doesn't take it! When a new value is selected it will be displayed and formatted. If i remove the format it works as expected

Answer

OnaBai picture OnaBai · Sep 30, 2013

@Bobby_D is right, the problem is that you did not specify the right date format: MM/dd/yyy.

BTW: Do you know that you can define it as:

<input data-role="datepicker" value="9/18/2013 12:34:56 AM" data-format="MM/dd/yyyy"/>
<script>
    kendo.init($("input"));
</script>   

Basically, if you can set all properties in the HTML input you just need to do one call to kendo.init for getting the elements initialized. So, you can even do kendo.init($("body"));. This is very useful when initializing most components from HTML.

EDIT: There are two different options in Kendo DatePicker:

  1. format: Specifies the format, which is used to format the value of the DatePicker displayed in the input. The format also will be used to parse the input.
  2. parseFormats: Specifies a lis of date formats used to parse the value set with value() method or by direct user input. If not set the value of the format will be used. Note that format option is always used parsing process.

It seems to me that you want to receive dates in one format and then display them in another. Then you should use parseFormats for the possible ones that you receive (might be more than one) and format for the ones displayed in the widget.

You code would be:

$(':input[data-kendoDatePicker=true]').kendoDatePicker({
    format:        "dd MMM yyyy",
    parseFormats : [ "MM/dd/yyyy" ]
});

The code modified in here : http://jsfiddle.net/OnaBai/TQnny/1/

or in the alternative format:

<input id="datapicker" data-role="datepicker" value="9/18/2013 12:00:00 AM" data-format="dd MMM yyyy" data-parse-formats="MM/dd/yyyy"/>

and the JS for initializing it:

kendo.init($("init"));

The code modified in here : http://jsfiddle.net/OnaBai/TQnny/2/