I have an Excel File with a column in this format “MM/dd/yyyy HH:mm” with column type General, I need to filter the file and only process the records created after yesterday’s date at 10:00 AM.
If my excel file column looks like this (General Type Column):
@rjackson12
As a filter result can also be empty, we should handle defensive the empty filter result. CopyTodDataTable would throw an exception in that case
or dynamicly to every yasterday 10:00:
now.Date.AddDays(-1).AddHours(10)
then we can filter with a LINQ:
(From d In dtData.AsEnumerable
Where Not If(IsNothing(d(0)), True, String.IsNullOrEmpty(d(0).toString.Trim))
Where CDate(d(0).toString.Trim) >= FilterDate
Select d).toList
And handle defensive the empty filteresult incase it occurs:
dtFiltered = drFiltered.CopyToDataTable
Kindly note: 10/2/2021 9:29 is filtered out as it is before yesterdays 10:00
In case of variances, we can adopt also the date conversion function e.g. uising DateTime.ParseExact(… instead of CDate(
in your data is a columnvalue “Shabreckae”. As this is not a valid datetime string it cannot be converted and is throwing an issue.
Have a check on your data. We could adopt the LINQ and filter out all invalid datetime strings. But it is more recommended to have a requirement analysis on:
what data variances os to expected
what can done on the side which is creating this data
The column type in Excel is general as I mentioned in my original post.
I cannot make any changes on the site creating the data and there will be no variances, it will only be type general and looks like this “11/1/2021 14:53”