Hi Iamable to extract the dates from the string ,dates are in different formats like–01042024,08-12-23,10/12/2024, here i want apply a condition , if my extract should be in between 30-45 days , if it is not in between that days than it should delete all rows from the excel, i apply the condition for this–If(CDate(ExtractedDate).AddDays(30) > DateTime.Now AndAlso CDate(ExtractedDate).AddDays(-45) < DateTime.Now, dtFiltered, New System.Data.DataTable())----- but it show me error when it receive a date like this–01042024, suggest me a good condition or correct this if my condition is wrong
Hi @mkankatala i receive the error by this-- Assign: String ‘’ was not recognized as a valid DateTime.---- this is due to this date like 01042023
Help here how solve it
Dim dateFormats As String() = {"ddMMyyyy", "MM-dd-yy", "MM/dd/yyyy"}
Dim today As DateTime = DateTime.Now
dtFiltered = (From row In dt.AsEnumerable()
Let dateStr = row("ExtractedDate").ToString()
Let parsedDate = (Function()
For Each format As String In dateFormats
Dim result As DateTime
If DateTime.TryParseExact(dateStr, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, result) Then
Return result
End If
Next
Return DateTime.MinValue
End Function).Invoke()
Where parsedDate <> DateTime.MinValue AndAlso parsedDate.AddDays(30) > today AndAlso parsedDate.AddDays(-45) < today
Select row).CopyToDataTable()