How to identify date format from a string

I have lots of date coming in different formats. It could come in dd/mm/yyyy or mm/dd/yyyy i need a way to identify which is month and which is day. for example “3/11/2023” comes in dd/mm/yyyy format but the same date could come in “11/03/2023” in both cases bot has to identify ‘3’ as the date and ‘11’ as the month. Please let me know any possible solutions. i was using this but it is identifying the same in the wrong way “Date.TryParseExact(Test,“dd/MM/yyyy”,CultureInfo.InvariantCulture,Globalization.DateTimeStyles.None,Nothing)” Thanks in advance

Hi @Ajith209

Try this:

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

this will take whether date will be in the format dd/MM/yyyy or MM/dd/yyyy.

Hope it helps!!

Hi Parvathy, When i am keeping all the expected formats in an array. bot is taking 11/03/2023 as dd/mm/yyyy but it should take it as mm/dd/yyyy. Essentially wrong format is coming true.

@Ajith209

Are you taking the data from excel file. Please specify.

Regards,

i am taking the data from an excel file.

@Ajith209

If possible share the excel file.
Regards,

When working with the format array, we have to meet the method signatures and have also to serve the
DateTimeStyles parameter

grafik

Indeed this can happen. When this is to expect a quick question on how it is handled by a human can help.

Without the knowledge what is month / day part a human also cannot decide the format e.g. 05/11/2023. And has to handle this case special.

Same we can bring it into the bot

1-12/1-12/XXXX | x- a digit dates cannot be rated
on other dates we can detect the day part as it is greater then 13 cannot be the month

So, we could bring this format validation check in place e.g. by using a Regex / LINQ
other dates we would routed back / sorting out, as the date is uncertain

unfortunately i won’t be able to share excel as it’s confidential data

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