Convert String to Date Time with varying formats

Dear Community,

I have a input string, which I want to convert to datetime. Normally I could use Assign = Date.ParseExact(“17.03.2020”,“dd.MM.yyyy”,System.Globalization.CultureInfo.InvariantCulture). That works fine. The Challenge is, that in the end I need the format “dd.MM.yyyy”, but the input String can be in a different format. For example “5.3.2020” or “15.3.2020” or “1.10.20” and so on. This causes errors

Is there any solution for this? Any oppurtunity to recognize the format and to convert it?

Thank you very much!

it is always “day.Month.year”

If MyStr is the string containing your input date, you can use:

Date.ParseExact(String.Concat(MyStr.Split(".")(0).ToString("D2"), "-", MyStr.Split(1).ToString("D2"), "-", MyStr.Split(2).ToString("D4")), "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture).

@Jonathan_Langer

Please check as below

Mark as solution if this helps

Thanks

Assign activity warns me, that Int can not be converted to char and open strict on cannot convert from string to char.

what did you right in the second assign activity? or what datatype is the TimeSplit

@Jonathan_Langer

Second Activity is to TimeSplit which is a String (Array of String) type

TimeSplit = currentTime.Split("."c)

Hope this helps

Thanks

1 Like

have a look here on how to handle multiple formats with a single DateTime.ParseExact

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