How to check if a string value is in correct date format (dd/MM/yyyy)?

Greetings,

In my current Studio project I am trying to check if a string value is in the correct date format of (dd/MM/yyyy).

Currently I am using this to return a boolean and see if it is in valid format:
Date.TryParseExact((dt_PromoSheet(0(item).ToString.Trim),“dd/MM/yyyy”,Nothing,Globalization.DateTimeStyles.None,Nothing)

The string value I’m checking is “05/24/2022”, which should return True

However, this TryParseExact method is also returning the time as seen below:
“Date: 05/24/2022 00:00:00 is NOT in valid date format”

How do I just check the date and ignore the time?

05/24/2022 is MM/dd/yyyy format and not:

maybe you check for multiple formats in one go

arrFormats = {"dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss"}
DateTime.TryParseExact( "YourString", arrFormats , CultureInfo.InvariantCulture, DateTimeStyles.None, dtParsed).toString

Add the hh:mm:ss into your format. It’s faulting because the format doesn’t match the data it’s provided.

Thanks for your response.

The line you provided works when I have the second format with the HH:mm:ss, but when I remove it I get the same 05/24/2022 00:00:00. Any thoughts on how to just get date as result?

just keep a few things in mind

With TryParseExact we check if a parsing will be possible, as mentioned we can configure multiple formats

With parseExact we do parse a string into a datetime variable. Similar to the TryParseExact we also can configure to support multiple formats in one go:

arrFormats - as above
DateTime.ParseExact(YourStringVar, arrFormats, cultureinfo, DateTimeStyles.None)

A datetime variable has its internal structure also along with the time portion. We do not touch the structure schema and also do not try to reformat it.

When we do need another formatted string we use YourDateTimeVar.ToString(“YourAlternateFormatString”)

it sounds like you are trying / expecting to touch the datetime structure. As mentioned we do not touch

Conclusion:

  • TryParseExact
  • ParseExact
  • ToString (if another format / representation is needed)

Thanks ppr, I actually found my issue. All the commands were correct and working, but the date value I read from excel had “00:00:00” to appended to it as well. As seen as such:
image

My project is converting an excel sheet into a datatable, and excel assigned the time to the date value even though it wasn’t present in my input. Have you ever experienced this? I made a custom format as just “MM/dd/yyyy” without time yet it was still appended.

what visually is displayed in Excel is not mandatory the value within the datatable. This is common, when the values recognized as a date when read range it.

Have a look here:

@tom_mccaffrey From where you are reading the data to datatable. If it is from excel then make sure you enable Preserve Format in read range activity. Below workflow for ref

Example.zip (10.1 KB)

Capture

Also, if you are using for each row in data table activity the exp should be in assign activity like below

Variable of type boolean Output = DateTime.TryParseExact(CurrentRow(0).ToString, "M/dd/yyyy",  CultureInfo.InvariantCulture, DateTimeStyles.None, Nothing)

0 represents the column index

That’s exactly what I needed! Thanks ushu, toggling PreserveFormat worked.

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