Format Dates in String Array

Hello,

How do i format string array items(all items are dates) currently in “MM/dd/yyyy” into “dd/MM/yyyy”. I need all my dates in a array even after conversion. Many thanks :slight_smile:

Hi.

Could always use a ForEach loop with an assign inside, but my approach is usually linq.
For linq, you would use the .Select()
Then, reformat the dates as desired.
arrVar.Select(Function(d) Convert.ToDateTime(d).ToString("dd/MM/yyyy") ).ToArray

For extra robustness, you might want to make sure all values are dates, maybe with an if activity using this condition:
arrVar.All(Function(d) IsDate(d))

Hope that helps.

EDIT: correction made. Should be Convert.ToDateTime()… can also use vb like CDate()

Regards.

3 Likes

Hi,
Could you please provide an example or elaborate more thank you

Sorry about that. I don’t have a screenshot on me right now, but you can simply use an Assign activity to assign the converted array.

Assign activity: arrVar = arrVar.Select(Function(d) Convert.ToDateTime(d).ToString("dd/MM/yyyy") ).ToArray

The way the .Select() works is it initializes ‘d’ by doing Function(d) to be used inside the parentheses, which represents all iterations/values in the array. It essentially loops through all values, then using Convert.ToDateTime(), it changes the values to use a different date format.

If you provide any images or code snippets that you have problems with, I can help you identify the issue.

Regards.

Hi,
The .toArray at the last part is not showing up when i type .

For my codes i read range of excel and filter a column and then output it as string. After that i split it into a array.
image

Did you try typing it out anyway to see if it fixes the blue mark?

If you get the blue mark, can you show what that says?

Thanks.

Hi,
There is no blue mark after i type it out but theres an error when i run the process
image

Column that’s bring filtered
image

Looks like the dates are already in “dd/MM/yyyy” format in that file. So are you only wanting to convert them if they are not in that format, because this might make it more complicated.

However, what you can do is simply surround the Assign activity with an If activity. So place the Assign inside an If (or use a Decision). Then, for the condition, use the .All() which will check if all values are in a DateTime format.

If condition: finalArray.All(Function(d) IsDate(d))

That way, it will only try to convert the array to “dd/MM/yyyy” if there are invalid date formats. However, if all dates are under day 12 where the “dd/MM/yyyy” and “MM/dd/yyyy” are all valid date formats, then this approach would not work - if that makes sense.

Regards.

Hi yes the dates are in the format that i want in the excel file but when i display them it shows up as “mm/dd/yyyy”
image

Hi
I did the if activity but its not working.
image
image

I see.
Can you show the properties of that Input Dialogue box? It looks like it is converting the dates to DateTime types. You will want them as String types.

image

That looks good. I think your finalArray has the dates stored like datetimes, and when you tried to convert the format, there is one value that’s not a date, like the first value maybe. Maybe that’s why you used .Skip(1)?

Anyway, you can undo the If activity, so it’s just the activity. And, in the assign, you can only convert the dates if they are datetimes.
So, the modification would look like this:

Assign activity: finalArray = finalArray.Select(Function(d) If(IsDate(d), Convert.ToDateTime(d).ToString("dd/MM/yyyy"), d) ).ToArray

Basically, it ignore all non-dates

You can also place the .Skip(1) before the .Select() if you want to skip the first value.

If that doesn’t help, you can output all the values in finalArray in a WriteLine to verify what values are there like this:
String.Join(",", finalArray)

Regards.

1 Like

Oh my gosh its working thank you so much for you help :grin:
image

1 Like

yay!

1 Like

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