Convert any string to Date

Hi Team,

I wanted to convert a string to date but I am not sure about input format of it.
for example it might be in dd.MM.yyyy or d.MM.yy or dd/MM/yyyy or any other format.

Hi @Shaik.Yezdani

Try this:

Output = DateTime.ParseExact(Input,{"dd.MM.yyyy","d.MM.yy","dd/MM/yyyy"},System.Globalization.CultureInfo.InvariantCulture)

Output is of DataType System.DateTime

If you want you can specify the date formats within double quotes within curly braces.

Hope it helps!!

@Shaik.Yezdani

You can give all the possible formats that you know like this and convert to date

DateTime.ParseExact(DateString,{"dd.MM.yyyy","d.MM.yy","dd/MM/yyyy"},System.Globalization.CultureInfo.InvariantCulture,Nothing)

cheers

Hi @Shaik.Yezdani

→ Create a Array of string datatype variable called ArrFormats and store all formats in the variable.

- Assign -> ArrFormats = {"dd.MM.yyyy","d.MM.yy","dd/MM/yyyy"}

→ Then create a DateTime datatype variable to store the value.

- Assign -> DateTime = DateTime.ParseExact(Input.toString,ArrFormats,System.globalization.Cultureinfo.InvariantCulture)

→ Input is the string variable which contains the string date.

Hope it helps!!

Hi @Shaik.Yezdani

Try this

DateTime.ParseExact(InputDate,{"dd.MM.yyyy","d.MM.yy","dd/MM/yyyy"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None)

Cheers!!

You can’t always deduce the pattern so if you try to accept any format you are going to have errors. If we take 10/12/2024 as an example, if we don’t know the format we can’t be sure which is the day and which is the month. Might be better to accept only certain ones and leave others to manual handling/checking.

Hey @Shaik.Yezdani
try this vb.net script. You can use invoke code activity:

Dim dateFormats As String() = New String() {"dd.MM.yyyy", "d.MM.yy", "dd/MM/yyyy", "MM/dd/yyyy", "yyyy-MM-dd", "d-MMM-yy", "MMM d, yyyy"}
Dim tempDate As DateTime

For Each format As String In dateFormats
    If DateTime.TryParseExact(dateString, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, tempDate) Then
        parsedDate = tempDate
        Exit For
    End If
Next
  • Input Argument: dateString of type String that contains the date string you want to convert.
  • Output Argument: parsedDate of type DateTime that will store the converted date.
1 Like

Hi @Shaik.Yezdani

DateTime.ParseExact(InputString,{"dd.MM.yyyy","d.MM.yy","dd/MM/yyyy"},System.Globalization.CultureInfo.InvariantCulture)

Cheers!!

Hi,

Try this

CDate(Your Variable.ToString)

Thank You :slightly_smiling_face:

This is not properly converting, some times it is taking month as date

@lrtetala I am getting the error as

“Value of type ‘String()’ cannot be converted to ‘String’. The selected value is incompatible with the property type. Argument ‘Value’: BC30311: Value of type ‘String()’ cannot be converted to ‘String’. The selected value is incompatible with the property type. in UiPath”

@Shaik.Yezdani

Have you tried this

DateTime.ParseExact(InputDate,{"dd.MM.yyyy","d.MM.yy","dd/MM/yyyy"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None)

@lrtetala tried, but still getting the same, till yesterday it was fine, but I have opened source code today and got this error.

@Shaik.Yezdani

It is working

DateTime.ParseExact(Input_Date,{"dd.MM.yyyy","d.MM.yy","dd/MM/yyyy"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None)

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