Date Field Binding In Kendo Grid

Introduction

 
In this blog we are going to see how to bind the date field in kendo grid.
 

Kendo Grid

 
Create a HTML page, and write the below code in it, 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2019.2.514/styles/kendo.common-material.min.css" />  
  6.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2019.2.514/styles/kendo.material.min.css" />  
  7.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2019.2.514/styles/kendo.material.mobile.min.css" />  
  8.   
  9.     <script src="https://kendo.cdn.telerik.com/2019.2.514/js/jquery.min.js"></script>  
  10.     <script src="https://kendo.cdn.telerik.com/2019.2.514/js/kendo.all.min.js"></script>  
  11.       
  12.   
  13. </head>  
  14. <body>  
  15.         <div id="example">  
  16.             <div id="grid"></div>  
  17.   
  18.             <script>  
  19.                 $(document).ready(function() {  
  20.                     var movies = [  
  21.                         { "rank": 1,  "rating": 9.2, "DOR"new Date(), "title""The Shawshank Redemption" },  
  22.                         { "rank": 2,  "rating": 9.2, "DOR"new Date, "title""The Godfather" },  
  23.                 ];  
  24.                     $("#grid").kendoGrid({  
  25.                         dataSource: {  
  26.                             data: movies,  
  27.                             schema: {  
  28.                                 id: "rank",  
  29.                                 model: {  
  30.                                     fields: {  
  31.                                         title: { type: "string" },  
  32.                                         DOR: { type: "Date" },  
  33.                                         rating: { type: "string" },  
  34.                                          
  35.                                     }  
  36.                                 }  
  37.                             },  
  38.                             batch: true,  
  39.                             pageSize: 20  
  40.                         },  
  41.                         height: 550,  
  42.                         scrollable: true,  
  43.                         sortable: true,  
  44.                         filterable: true,  
  45.                         editable: "inline",  
  46.                         
  47.                         columns: [  
  48.                              
  49.                             { field: "title", title: "Name" },  
  50.                             { field: "DOR", title: "Release Date", format: "{0:MM/dd/yyyy}",  
  51.                         },  
  52.                             { field: "rating", title: "Rating" },  
  53.                             { command: ["edit""destroy"], title: " ", width: "250px" }  
  54.                         ]  
  55.                     });  
  56.                 });  
  57.             </script>  
  58. </div>  
  59. </body>  
  60. </html>  
From the above code you can notice we have a kendo grid with a movie datasource where the inline editing is enabled. In the movie data source we have a date time field; where in schema we need to mention the type as date.
 
As date of release is a Date field, we need to format it for better user readability.
  1. { field: "DOR", title: "Release Date", format: "{0:MM/dd/yyyy}",  
 
Now we bind the date field in kendo grid. When user is in edit mode we need to render the datepicker control to update the date, the datepicker control can be initialized from editor property.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title></title>  
  5.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2019.2.514/styles/kendo.common-material.min.css" />  
  6.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2019.2.514/styles/kendo.material.min.css" />  
  7.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2019.2.514/styles/kendo.material.mobile.min.css" />  
  8.   
  9.     <script src="https://kendo.cdn.telerik.com/2019.2.514/js/jquery.min.js"></script>  
  10.     <script src="https://kendo.cdn.telerik.com/2019.2.514/js/kendo.all.min.js"></script>  
  11.       
  12.   
  13. </head>  
  14. <body>  
  15.         <div id="example">  
  16.             <div id="grid"></div>  
  17.   
  18.             <script>  
  19.                 $(document).ready(function() {  
  20.                     var movies = [  
  21.                         { "rank": 1,  "rating": 9.2, "DOR"new Date(), "title""The Shawshank Redemption" },  
  22.                         { "rank": 2,  "rating": 9.2, "DOR"new Date, "title""The Godfather" },  
  23.                 ];  
  24.                     $("#grid").kendoGrid({  
  25.                         dataSource: {  
  26.                             data: movies,  
  27.                             schema: {  
  28.                                 id: "rank",  
  29.                                 model: {  
  30.                                     fields: {  
  31.                                         title: { type: "string" },  
  32.                                         DOR: { type: "Date" },  
  33.                                         rating: { type: "string" },  
  34.                                          
  35.                                     }  
  36.                                 }  
  37.                             },  
  38.                             batch: true,  
  39.                             pageSize: 20  
  40.                         },  
  41.                         height: 550,  
  42.                         scrollable: true,  
  43.                         sortable: true,  
  44.                         filterable: true,  
  45.                         editable: "inline",  
  46.                         
  47.                         columns: [  
  48.                              
  49.                             { field: "title", title: "Name" },  
  50.                             { field: "DOR", title: "Release Date", format: "{0:MM/dd/yyyy}",  
  51.                             editor: function (container, options) {  
  52.                         
  53.                         $('<input data-text-field="' + options.field + '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')  
  54.                             .appendTo(container)  
  55.                             .kendoDatePicker();  
  56.                     },  
  57.                         },  
  58.                             { field: "rating", title: "Rating" },  
  59.                             { command: ["edit""destroy"], title: " ", width: "250px" }  
  60.                         ]  
  61.                     });  
  62.                 });  
  63.             </script>  
  64. </div>  
  65. </body>  
  66. </html> 

Summary

 
So far, we have seen how to bind the date field in kendo grid and  format the date, then finally we saw how to initialize the datepicker control using editor column property when the user is in edit mode.
Next Recommended Reading Auto Fit Column In Kendo Grid