Get Max and Min Date in Column

Hey everybody,

I want get min and max of my date column “[RÇU]Date de réception (Date)” of my datatable ‘dt’.

Can you help me ?

Thank !

Hi,

Can you try the following expression?

Max

dt.AsEnumerable.Max(Function(r) DateTime.Parse(r(0).ToString))

Min

dt.AsEnumerable.MIn(Function(r) DateTime.Parse(r(0).ToString))

The above returns DateTime type. If you need string please add .ToString

Regards,

2 Likes

Thank man, but where i specify my column name ?

Hi,

Sorry, the following will work

dt.AsEnumerable.Max(Function(r) DateTime.Parse(r("[RÇU]Date de réception (Date)").ToString))

dt.AsEnumerable.Min(Function(r) DateTime.Parse(r("[RÇU]Date de réception (Date)").ToString))

Regards,

1 Like

Thanks again but i have error message.
‘String was not recognized as a valid DateTime.’

Hi,

Is there any blank cell or non-datetime string in the column?
If so, the following will work. Can you try this?

dt.AsEnumerable.Where(Function(r) DateTime.TryParse(r("[RÇU]Date de réception (Date)").ToString,New DateTime)).Max(Function(r) DateTime.Parse(r("[RÇU]Date de réception (Date)").ToString))

Or if the above expression doesn’t return expected result and your datetime format is not dd-MM-yyyy style etc, we need to modify the above expression.

Regards,

1 Like

You are the best, he work. But i need change datetime format to dd-MM-yyyy

Hi,

Can you add .ToString("dd-MM-yyyy") as the following?

dt.AsEnumerable.Where(Function(r) DateTime.TryParse(r("[RÇU]Date de réception (Date)").ToString,New DateTime)).Max(Function(r) DateTime.Parse(r("[RÇU]Date de réception (Date)").ToString)).ToString("dd-MM-yyyy")

Regards,

1 Like

Checkout if more assistance on analysing parsing validation issues is needed:

:ambulance: :sos: [FirstAid] Datatable: Debug & Analysis invalid DateTime Strings / String to DateTime Parsing Issues - News / Tutorials - UiPath Community Forum

1 Like

Perfect, but this expression return the min (‘12/04/2022’) when i should have ‘27/04/2022’

image

i try change Max → Min and is same

Hi,

it seems dd/MM/yyyy format. Can you try the following?

dt.AsEnumerable.Where(Function(r) DateTime.TryParseExact(r("[RÇU]Date de réception (Date)").ToString,"d/M/yyyy",System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None,New DateTime)).Max(Function(r) DateTime.ParseExact(r("[RÇU]Date de réception (Date)").ToString,"d/M/yyyy",System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None)).ToString("dd-MM-yyyy")

dt.AsEnumerable.Where(Function(r) DateTime.TryParseExact(r("[RÇU]Date de réception (Date)").ToString,"d/M/yyyy",System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None,New DateTime)).Min(Function(r) DateTime.ParseExact(r("[RÇU]Date de réception (Date)").ToString,"d/M/yyyy",System.Globalization.CultureInfo.InvariantCulture,DateTimestyles.None)).ToString("dd-MM-yyyy")

Regards,

DateTimestyles is not declared

import System.Globalization
import_systemglobalization

1 Like

Hi,

Can you try @ppr 's way or the following?

dt.AsEnumerable.Where(Function(r) DateTime.TryParseExact(r("[RÇU]Date de réception (Date)").ToString,"d/M/yyyy",System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimestyles.None,New DateTime)).Max(Function(r) DateTime.ParseExact(r("[RÇU]Date de réception (Date)").ToString,"d/M/yyyy",System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimestyles.None))

Regards,

1 Like

thank you so much !

thank you so much !!!

1 Like

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