Invalid Date check in code not working

NiallM

New Member
Hi all, I'm a new BP user and have a issue here where I'm trying to use C# code in BP to check to see if a date is valid.

My code is

DateTime dateTime;
string mydate = Year + Month + Day;
validDate = (DateTime.TryParseExact(mydate, "yyyyMMdd",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime));

I pass the Year; Month and Day as inputs from values already held in a data object as text.
My output is a flag called valid date.

When I run the code with month, day and date passed in with the value in the data object it is not working. (see image below)
However when I pass it in with hard coded values in quotes - the code works perfectly. i.e "11" "11" "2018"

Could anyone advise what I'm doing wrong?


View attachment Capture.PNG
 

VJR

Well-Known Member
Hi NiallM,

You code is working fine and there are no issues with the output.
The flag returns a False on an incorrect date and True on a correct one with some basic inputs.
View attachment 1542268758101.png

Did you try to show something in the screenshot when you mentioned "it is not working. (see image below)" ?
 

NiallM

New Member
Hi Vj, thanks for the reply.

Found I needed to add a Trim() around all the variables in the code section. So although the date looked like it was fine in data items; the Trim was necessary in order to get BP to accept it correctly.

In case anyone has a similar issue in future I amended my code to the below:

DateTime dateTime;
string mydate = y.Trim() + m.Trim() + d.Trim();
valid = (DateTime.TryParseExact(mydate.Trim(), "yyyyMMdd",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime));
 
Top