Error: String '' was not recognized as a valid DateTime

Hi everyone,

I’m encountering an issue while trying to convert dates in different formats into a specific format (dd/MM/yyyy). I have dates in the following formats:

  • 22/Oct/2024
  • 06/22/2023
  • 18.11.2023

When I attempt to convert them to the format 22/06/2024, I’m getting this error:Assign: String ‘’ was not recognized as a valid DateTime.

i am using this method : Convert.ToDateTime(“26/6/2024”).ToString(“dd/MMM/yyyy”) still it is throwing error

Could you try below script?

DateTime.ParseExact("06/22/2023", "MM/dd/yyyy", CultureInfo.InvariantCulture)

Hai
Thanks Your response
but i tried this method but it is not working
DateTime.ParseExact(“06/22/2023”, “MM/dd/yyyy”, CultureInfo.InvariantCulture)
i don’t have specific format i am getting diffarent formats but i want to convert "dd/mm/yyyy " this format only

@rajababu.mandapati,

Use this snippet. You can add all possible formats in the array and it will always give you output in “dd/MM/yyyy”

strOutputDate= DateTime.ParseExact(inputDateVariable, {"dd/MMM/yyyy","MM/dd/yyyy","dd.MM.yyyy"}, CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

Learning Resource:

**Added missed ToString format at the end.
**Added learning resource

Hi @rajababu.mandapati ,
Just add this format in the array and check once

Use this expression

Datetime.ParseExact(D_format.trim,{“yyyy.MM.dd”,“yyyy-MM-dd"," MM-dd-yyyy",“dd-MM-yyyy”},System.Globalization.CultureInfo.Invariantculture,System.Globalization.DateTimeStyles.None).ToString(“dd/MM/yyyy”)


Go on adding the different types of date formats. Hope this helps. Please check and update
Note: In the above expression D_format is nothing but input date

Hi @rajababu.mandapati

Can you try this

formattedDate = DateTime.ParseExact(dateString, {"dd/MMM/yyyy", "MM/dd/yyyy", "dd.MM.yyyy"}, 
                                           System.Globalization.CultureInfo.InvariantCulture, 
                                           System.Globalization.DateTimeStyles.None).ToString("dd/MM/yyyy")

Regards,

Hi @rajababu.mandapati

Try this way

Str variable = DateTime.ParseExact("Yourinput",{"dd/MMM/yyyy","MM/dd/yyyy","dd.MM.yyyy"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("dd/MM/yyyy")

Regards,
Gowtham K

Hi @rajababu.mandapati ,
Refer this workflow
ConvertDate.zip (2.5 KB)
image

in addition to above:
A date like: 7th May 2024 - 07-05-2024

As 5th July 2024 - 05-07-2024 is ambigious to 7th May 2024 - 05-07-2024
above approach cannot reliable handle all formats and prefers the first format match

We should keep this in mind. However in such a case, when ambiguous dates are expected, then we should detect and handle very carefully

thanks your information