How to transform this text?


#1

Hi all,

How could I transform the string “1 4 2018” into “01 04 2018”? (I mean dd mm yyy)

It is just an example. It could also be “12 3 2018” into “12 03 2018” or “3 11 2018” into “03 11 2018”.

Thanks a lot !


#2

Hi.

If it’s a date, I would suggest converting it to a datetime type. You can do this with DateTime.ParseExact()

Then, you can easily format the date any way you like.

If I get the syntax right, it will look like this:

DateTime.ParseExact("1 4 2018","d M yyyy", System.Globalization.CultureInfo).ToString("dd MM yyyy")

Regards.


#3

Thank you very much.

I will try and let you know.

Regards.


#4

Hello,

What if the format isn’t “1 4 2018” but “12 1 2018”?

I mean, we should configure it as if we do not know the right format.

Regards


#5

Hey @pal1910

It will still work because when you Parse the date by the format “d M yyyy”, the “d” and “M” are seen as the number just without the leading 0, so 1-31 or 1-12 will be seen correctly and converts it to a date type.

Here is a little test showing what I mean:

image

Regards.


#6

Thank you very very much.

Best regards