I tried to extract SAP data into datatable and got
so trying to filter out this way :
Curr_Date = DateAndTime.Today which returns “03/04/2024 00:00:00”
and then
dt_FilteredDT= dt1.AsEnumerable.Where(Function(x) Datetime.ParseExact(x(“Valid from”).ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture)> Curr_Date).CopyToDatatable
but this is throwing error : Assign: String ‘23.01.2019’ was not recognized as a valid DateTime.
in this ‘23.01.2019’ is coming from x(“Valid from”)
Any suggestion to filter out datatable using linq query so rows with future date will be extracted
Curr_Date = DateAndTime.Today.AddYears(-1)
' Filter the DataTable based on the date format "dd.MM.yyyy"
dt_FilteredDT = dt1.AsEnumerable.Where(Function(x) DateTime.ParseExact(x("Valid from").ToString(), "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture) > Curr_Date).CopyToDataTable()
It looks like the date format in your datatable column “Valid from” is in the format “dd.MM.yyyy” (e.g., ‘23.01.2019’). In your code, you’re trying to parse it using the “MM/dd/yyyy” format, which is causing the error.
check with expression
dt_FilteredDT = dt1.AsEnumerable.Where(Function(x) DateTime.ParseExact(x(“Valid from”).ToString, “dd.MM.yyyy”, System.Globalization.CultureInfo.InvariantCulture) > Curr_Date).CopyToDataTable
DateTime.ParseExaxt method with convert the givent input format parameter to the format MM/dd/yyyy which you can compare with Curr_Date. Even when your date format is dd.MM.yyyy it will converted to the format MM/dd/yyyy format and compare it with Curr_Date variable.
Curr_Date = DateAndTime.Today.AddYears(-1).ToString("MM/dd/yyyy")
' Filter the DataTable based on the date format "MM/dd/yyyy"
dt_FilteredDT = dt1.AsEnumerable.Where(Function(x) DateTime.ParseExact(x("Valid from").ToString(), "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture) > DateTime.ParseExact(Curr_Date, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)).CopyToDataTable()
what is input format in your sap application, is in your sap application you have more formats,Please put all your possible formats and what is format you want to compare of Curr_Date please give us more inputs on that