Hi, I’ve been looking for an expression to filter a column in a DataTable (read from excel workbook) based on time, which holds values written in yyyy/MM/dd HH:mm:ss format in each cell. I need to keep the date info, while filtration should be performed focusing on time alone.
for example,
→select datarow 0 and 1 (<8:00:00) and 3 and X (>20:00:00) and assign to dt_filtered
Since DataTable.Select method does not seem to work with timespan values, i have no idea how to compute this. Maybe in a LINQ expression I can convert Object values to DateTime and to TimeSpan, then finally pick values in a certain time slot?
You can loop through each record and check-
(new DateTime(2018,2,13,8,0,0).TimeOfDay - Convert.ToDateTime(row(“Time”).ToString).TimeOfDay).TotalSeconds >= 0
I have hard coded the Compare date here DateTime(2018,2,13,8,0,0). You can write code to fetch current date instead.
The same condition check query should work with linq as well with slight changes in syntax.