Determine most recent and oldest dates


#1

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.


#2

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


#3

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?


#4

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


#5

Thank you Arivu96 :slight_smile: