Determine most recent and oldest dates

Read in a datatable, one of the rows is a list of dates.

How would I determine which date was the oldest and which was the most recent? and how could I assign these dates as such?

Thank you.

Hi @robot123,

Max Date

DateTime maxDate = Convert.ToDateTime( ((from DataRow dr in dt.Rows orderby Convert.ToDateTime(dr["DateColumn"]) descending select dr).FirstOrDefault()["DateColumn"] ) )

Min Date

DateTime minDate = Convert.ToDateTime( ((from DataRow dr in dt.Rows orderby Convert.ToDateTime(dr["DateColumn"]) ascending select dr).FirstOrDefault()["DateColumn"] ) )

Regards,
Arivu

1 Like

Thank you Arivu. Would you mind explaining the code a little bit? If I have my dates in the datatable assigned ‘DateTable’, how would I go about writing this?

ok here dt ->Data Table

using linq i am getting the min/max date.
dr ->DataRow
i used order by Column Name [“DateColumn”] Given ascending /descending
FirstOrDefault-> it will return the First value from the data table

[“DateColumn”] → Column Name
Convert.ToDateTime-> convert the value to date time.

Regards,
Arivu

Thank you Arivu96 :slight_smile:

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