How to filter dates in data table .. when date format is string


Can someone help me in filtering dates.

I want to filter below data table with creation date … creation date type is string

Condition is filter → creation date in DT should be greater than current date -30 days(05/08/2023) … that means from below table I want to get records of (05/22/2023 and 05/17/2023)

I tried below but it is not working

→ I got Target date = current date-30 [ d30daysback = (today.AddDays(-30)) ]
→ Converted to string [ str30daysBack = Convert.ToDateTime(d30daysback).ToShortDateString ]

→ Filterd like below

Find below attached excel

Report.xlsx (545.2 KB)



You should not be doing by converting to string

Please use it like a date only to filter dates

Try usingn nOw.adddays(-30) only or use Cdate(now.adddays(-30).ToString("MM/dd/yyyy"))

Alternately you can use linq to filter as well

dt = Dt.AsEnumerable.Where(function(x) Cdate(x("Current Date").ToString) >= Cdate(now.adddays(-30).ToString("MM/dd/yyyy"))).CopyToDataTable



@Anil_G linq worked … Thank you very much

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.