Filter dt based on Month

Hi ,

Here is my dt

Compliance Date User Bot Modified Date
02/13/2020 19:28:35 abercrombieandfitch_user 06/12/2024
2/13/2020 19:28 abercrombieandfitch_user 06/03/2024
02/13/2020 19:28:35 abercrombieandfitch_user 06/03/2024
I need to filter and take all the rows where Bot Modified Date is previous month . please help

I need to filter out all the rows where Bot Modified Date is previous month . please help

@tharani.natarajan

please try this in assign

dt = dt.AsEnumerable.Where(function(x) Cdate(x("Bot Modified Date").ToString).Month.Equals(Now.Addmonths(-1).Month)).CopyToDataTable

cheers

Give a try at:
dtFiltered =

(From d in dtData.AsEnumerable
Let ds = d("Bot Modified Date").toString.Trim
Let dp = CDate(ds)
Where dp.ToString("MM-yyyy").Equals(now.AddMonths(-1).ToString("MM-yyyy"))
Select r = d).CopyToDataTable

Handling empty result:

When DateParsing has to be adapted:

Kindly note:

  • we used the toString(“MM-yyyy”) trick to simply the month check
  • still the of doing date compares on DateTime DataType base is valid and should be applied
1 Like

to elaborate more on this:

today: 12 June 2024
check date 05 May 2023

today.AddMonths(-1) = 12 May 2024

A month (5) on month (5) will result to true
But: 05 May 2023 is not previous month (May 2024)

So we used the String trick to bring quickly also the year into the check

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