Using a Criteria to Obtain a Date Using a Specific date Format

In my scenario, I have an ID which is of format (ID_ddMMyyyyhhmm), so an example of an ID created on the 17th February 2022 at 11:44pm would read ID_170220222344. In my code the automation is required to pick out ID’s from several different ID’s within a table which are within the format ‘ddMMyyyyhhmm’. My question is: What could be done to verify if the ID, given as a string with the format (ID_ddMMyyyyhhmm) Is a valid date? using the format ‘ddMMyyyy’ to search through the table of ID’s and select the correct ID’s? For example, the automation must be able to obtain the ID (ID_170220222344) but ignore the ID (ID_321320231421)

1 Like

You can use DateTime.TryParseExact() to verify if it’s a valid date and time or not.

DateTime.TryParseExact(id.Substring(3), "ddMMyyyyHHmm", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, Nothing)

For example if you have a data table named dt and the IDs are in column “ID”, you can get the rows with valid dates like this:

dtFiltered = dt.AsEnumerable.Where(Function(r) DateTime.TryParseExact(r("ID").ToString.Substring(3), "ddMMyyyyHHmm", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, Nothing)).CopyToDataTable

image

1 Like