How to format a date in the Grid
3 Answers 1684 Views
Bill
Top achievements
Rank 1
Bill asked on 09 Feb 2012, 01:29 AM

What is required to display a date in format mm/dd/yyyy
I have defined the grid table as shown below.

<table id="CaseProgramGrid">
     <thead>
         <tr>
             <th data-field="clientLastName">@CaseProgramLabels.ListHeader_LastName</th>
             <th data-field="clientLastName">@CaseProgramLabels.ListHeader_FirstName</th>
             <th data-field="clientIdentifier">@CaseProgramLabels.ListHeader_ClientId</th>
             <th data-field="clientCaseIdentifier">@CaseProgramLabels.ListHeader_ClientCaseId</th>
             <th data-field="programType">@CaseProgramLabels.ListHeader_ProgramType</th>
             <th data-field="clientCaseStartDate">@CaseProgramLabels.ListHeader_StartDate</th>
             <th data-field="clientCaseStartDate">@CaseProgramLabels.ListHeader_EndDate</th>
             <th data-field="caseProgramStatus">@CaseProgramLabels.ListHeader_CaseProgramStatus</th>
             <th class="action">Actions</th>
         </tr>
     </thead>
     <tbody>
     @foreach (var item in Model.CaseProgramListItems) {
         <tr>
             <td>
                 @Html.DisplayFor(modelItem => item.ClientLastName)
             </td>
             <td>
                 @Html.DisplayFor(modelItem => item.ClientFirstName)
             </td>
             <td>
                 @Html.DisplayFor(modelItem => item.ClientIdentifier)
             </td>
             <td>
                 @Html.DisplayFor(modelItem => item.ClientCaseIndentifier)
             </td>
             <td>
                 @Html.DisplayFor(modelItem => item.ProgramType.Name)
             </td>
             <td>
                 @Html.DisplayFor(modelItem => item.ClientCaseStartDate)
             </td>
             <td>
                 @Html.DisplayFor(modelItem => item.ClientCaseEndDate)
             </td>
             <td>
                 @Html.DisplayFor(modelItem => item.CaseProgramStatus.Name)
             </td>
              
             <td class="action">
                 <ul>
                     <li>@Html.ActionLink(@CommonLabels.ActionLink_View, "Tasks", "Client", new { area = "CasePrograms", caseProgramId = item.CaseProgramId }, null)</li>
                 </ul>
             </td>
 
         </tr>
     }
     </tbody>
 </table>

And the kendo grid initialize as below. Question1 :  But receive a jscript error. 'Unable to get value of the property 'sortable': object is null or undefined' What is wrong? Question 2: Do need to specify every column in the columns: collection just to format 2 of the columns in the grid?

@section Script
{
 <script>
     $(document).ready(function () {
         $("#CaseProgramGrid").kendoGrid(
         
            dataSource: {
                group: {
                    field: "LastName",
                    dir: "asc"
                }
            },
            height: 360,
            groupable: true,
            scrollable: true,
            sortable: true,
            pageable: true,
            columns: [{ field: "LastName" }, { field: "FirstName" }, { field: "ClientIdentifier" }, { field: "ClientCaseIndentifier" }, { field: "ProgramType" }, { field: "ClientCaseStartDate", template: '#= kendo.toString(ClientCaseStartDate,"MM/dd/yyyy") #' }, { field: "ClientCaseEndDate", template: '#= kendo.toString(ClientCaseEndDate,"MM/dd/yyyy") #' }, { field: "CaseProgramStatus"}]
         });
     });
</script>
I also need to format dates in a grid.  I want to remove the time stamp and just show the date.

I tried this:
            { field: "DueDate", title: "Due Date", template: '#= kendo.toString(toDate(DueDate), "MM/dd/yyyy")#' },

Which I found on the Kendo Demo: 
http://www.kendoui.com/blogs/teamblog/posts/12-02-20/getting_started_with_kendo_ui_and_openaccess_orm.aspx 

But I get a javascript error "toDate not found"

The Webservices Crud example has

                    { field: "UnitPrice", format: "{0:c}", width: "150px" },

So I tried 
  • format: "{0:d}" 
  • format: "{d}"
  • format: "MM/DD/YYYY"
but nothing works.
The date renders like "Feb 20 2012 12:00 AM"

Note: I am having the webservice return the date in type string because if I return it of type date I get epoch style: "/Date(13315645000000)/"
- by
Jeremy
Top achievements
Rank 1
on 25 Feb 2012, 12:00 AM

3 Answers, 1 is accepted

Sort by
0
Jonas
Top achievements
Rank 1
answered on 03 Jun 2012, 11:09 AM
0
Lincoln
Top achievements
Rank 1
answered on 26 Jul 2017, 08:51 PM

This is a late response, but there is still not a good answer:

Jeremy, you were so close. Try this:

 { field: "DueDate", title: "Due Date", template: '#= kendo.toString(kendo.parseDate(DueDate), "MM/dd/yyyy")#' },

0
Peraiah
Top achievements
Rank 1
answered on 18 Aug 2017, 07:55 AM
This might helpful,
columns.Bound(date=> date.START_DATE).Title("Start Date").Format("{0:MM dd, yyyy}");
Tags
Grid
Asked by
Bill
Top achievements
Rank 1
Answers by
Jonas
Top achievements
Rank 1
Lincoln
Top achievements
Rank 1
Peraiah
Top achievements
Rank 1
Share this question
or