How to get previous closeset date from DataTable Using LinQ from excel sheet

Date.xlsx (10.2 KB)
I have this excel sheet. Now based upon input, I need to fetch closeset previous date.
one expert suggested me this
dt.AsEnumerable.Select(Function(r) DateTime.ParseExact(r(“Column”).ToString,“dd-MM-yyyy”,System.Globalization.CultureInfo.InvariantCulture)).OrderBy(Function(d) d).Where(Function(d) d<targetDate).Last.ToString(“dd-MM-yyyy”)

But this is not working because when reading data from excel, I am getting date in this format like 16-06-2022 00:00:00.

I pass input date as 25-06-2022, it should give me 3rd row that is “23-06-2022” as output.
Experts please help me


Try with this query, actually it fails because of the datetime for your passing in. Print the datetime once to get the format and use it in the query

dtbInput.AsEnumerable.Select(Function(r) DateTime.ParseExact(r("Check").ToString,"M/d/yyyy hh:mm:ss",System.Globalization.CultureInfo.InvariantCulture)).OrderBy(Function(d) d).Where(Function(d) d<  DateTime.Now.Date).Last.ToString()

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