How to select the largest and smallest dates from dates in an array of strings

Hello,

I need a little help with this logic… i have an array of date in the format 12JUN2020 thats ddMMMyyyyy … i need a sample sequence to help me output the largest and smallest dates in two different string varaibles.

Regards

Do you have an array of strings or array of datetimes?

If its Array of Datetime - you can use the link below:

If not, i would recommend converting the array of string to array of datetime first then looking at the above.

1 Like

@TimK Hello, thanks … so Sorry its an Array of strings… i just edited the title of the topic now… thanks, I have an array of strings

So you can try convert array to array of datetimes:

StringArray.Select(date => DateTime.Parse(date)).ToArray();

Give it a try :smile:

1 Like

@David_Oku An Extension of @TimK 's solution would be to use OrderBy and then get the First and the Last Date :

To get the First Date:
StringArray.Select(Function(x)DateTime.Parse(x.Trim)).ToArray.OrderBy(Function(x)x).First

To get the Last Date:
StringArray.Select(Function(x)DateTime.Parse(x.Trim)).ToArray.OrderBy(Function(x)x).Last

1 Like

Was just thinking that as i posted the conversion haha :slight_smile: :ok_hand:

1 Like

@supermanPunch @TimK Hello here is my array of string here string[12] { “20200320”, “20200320”, “20200320”, “20200320”, “20200321”, “20200321”, “20200321”, “20200321”, “20200322”, “20200322”, “20200322”, “20200322” }

using this StringArray.Select(Function(x)DateTime.Parse(x.Trim)).ToArray.OrderBy(Function(x)x).First

i get the error Can not assign ‘ArrayOfDateItem.Select(Function(x)DateTime.Parse(x.Trim)).ToArray.OrderBy(Function(x)x).First’ to ‘FirstDate’.

FirstDate is a datetime Variable

@David_Oku Is it a Validation error ?

1 Like

Nope…not a validation error…its an error that occurs when I run it

@David_Oku I guess the date format’s giving the error :

Check with this :

strArray.Select(Function(x)DateTime.ParseExact(x.Trim,“yyyyMMdd”,System.Globalization.CultureInfo.InvariantCulture)).ToArray.OrderBy(Function(x)x).First

1 Like

Glories!!! It worked …Super Mans Punch!!! Thanks a lot… I bet if Rpa was justice league and you are super man then i am gonna be robin because i definitely cant be batman…
Thanks @supermanPunch Thanks @TimK :sweat_smile: :sweat_smile: :sweat_smile:

2 Likes

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