Filter datatable date if monday

Hello there.
This might be an easy one but i coudnt make it work yet:
Need to filter a datatable column (as type date) at the condition:
If the day is monday then
Filter monday, sunday, saturday and friday dates.
Lets say monday is 28/09
Then the filtered output datatable must have
28/09, 27/09, 26/09, 25/09.

Tried using the default datatable but it doesnt work good.

LINQ solution would help but I wanted the cleanest way if possible.

Thanks in advance

Hi @Meu_Eu_Nando

Could you share the Input excel data and output.

hi @Meu_Eu_Nando
can you please try with the below invoke vb.net code

Imports System
Imports System.Linq

Public Class Main
Public Shared Sub Main()
’ Input date
Dim inputDate As DateTime = DateTime.Now ’ You can replace this with your variable date

    ' Check if the input date is Monday
    Dim isMonday As Boolean = inputDate.DayOfWeek = DayOfWeek.Monday
    
    If isMonday Then
        ' Calculate the dates for the previous Friday, Saturday, and Sunday (T-3)
        Dim previousFriday As DateTime = inputDate.AddDays(-3)
        Dim previousSaturday As DateTime = inputDate.AddDays(-2)
        Dim previousSunday As DateTime = inputDate.AddDays(-1)
        
             Else
    End If
End Sub

End Class
Hope it works!!!

Will post here tomorrow, no acess to machine right now

Good. I’ll test
Also there’s another condition i forgot to mention:
If it is not monday then
Filter by the current day and the last day (d0 and d-1)

If it would fit in the same vb.net would be great.

Anyway i’ll test tomorrow and see if your first vb.net works.
Thanks in advance

Hi Please find the update code for your new query. In the Else part i have added additional condition’s.
Check if the input date is Monday
Dim isMonday As Boolean = inputDate.DayOfWeek = DayOfWeek.Monday

If isMonday Then
    ' Calculate the dates for the previous Friday, Saturday, and Sunday (T-3)
    Dim previousFriday As DateTime = inputDate.AddDays(-3)
    Dim previousSaturday As DateTime = inputDate.AddDays(-2)
    Dim previousSunday As DateTime = inputDate.AddDays(-1)
    
         Else

Dim Currendate as datetime = DateTime.Now
Dim previousDate As DateTime = inputDate.AddDays(-1)

End If

End Sub


End Class
[/quote]
Hope it helps!!!
1 Like

Nice @Baskar_Gurumoorthy
Will try and respond as soon as possible. :slight_smile: