Filter grid on DateTime column

1 Answer 1045 Views
Filter Grid
Taras
Top achievements
Rank 3
Iron
Iron
Veteran
Taras asked on 19 May 2022, 06:05 PM

What is required to filter a grid on a DateTime without specifying the time?
i.e.: I want to see all records for a date.  All for 2022-05-18 regardless of the time.

Or do you have to specify the date using identifying greater than and less than?  i.e.: > 2022-05-17 and < 2022-05-19

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 24 May 2022, 12:11 PM

Hi Taras,

You could achieve the desired result as per the following approach:

  • Create a Model property which contains only the date portion of the datetime value:
public class GridViewModel
{
  public DateTime? OrderDate { get; set; }
  public DateTime? OrderDateNoTime { get; set; } //Only Date without the time portion
}

 

  • Bind the Grid column to the newly created field and use the ClientTemplate() option to display the datetime field instead. As a result, the Grid data will be filtered by "OrderDateNoTime" while displaying the original datetime field "OrderDate".
@(Html.Kendo().Grid <GridViewModel>()
  .Name("grid")
  .Columns(columns =>
  {
    columns.Bound(p => p.OrderDateNoTime)
    .ClientTemplate("#= kendo.toString(OrderDate, 'yyyy-MM-dd hh:mm:ss') #");
  })
  ...
)

Alternatively, check out the following KB article that demonstrates how to transform the filter expression when the date column is filtered:

https://docs.telerik.com/kendo-ui/knowledge-base/filter-by-date?_ga=2.131560523.517684514.1653286993-303039577.1625234276#transforming-the-filter

Also, this topic is discussed in the following forum threads:

https://www.telerik.com/forums/date-filtering-in-grid

https://www.telerik.com/forums/filter-datetime-column-only-by-date

https://www.telerik.com/forums/grid-filter-datetime-independent-of-time

I hope that helps.


Regards, Mihaela Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Taras
Top achievements
Rank 3
Iron
Iron
Veteran
commented on 24 May 2022, 01:17 PM

Thanks Mihaela, I'll try these out.
Tags
Filter Grid
Asked by
Taras
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Mihaela
Telerik team
Share this question
or