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”
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?
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:
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.
@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