TryParseExact is bool
Result will return no matter what. Valid date or the year 0001
This is a small section of code. those vars are needed
The problem was these dates are file names and I forgot to get just the file name and look for a date. This was not working because the code picked up some numbers in the path. Removed the path and file extension and now it works. This particular time, the year was being set as 0117 or something like that.
Also, why is everyone talking like they have been eating Tide pods?
Working code
string f = Path.GetFileNameWithoutExtension(file);
f = Regex.Replace(f, "[^0-9]", "");
if (f.Length >= 6)
{
try
{
bool z = DateTime.TryParseExact(f.Substring(0, 6), "yyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result);
string y = result.ToString();
if (z)
{
return result.ToString(dateformat);
}
}
catch (Exception ee)
{
return null;
}
}
I assumed everyone would know this was just a small section out of a method.
variable names do not document intention. - The unknowns are easy to assume.
code does not need to rely on exception handling. - When I write, I try catch. Yes, I threw the catch in the abyss but not always. Sometimes I write code and then remove part of it and sometimes I don't clean up all the way.
If you are going to use a regex expression, you might as well have it capture the yyMMdd value as a single capture group. - I fucking HATE using regular expressions and have ZERO problems with going the long way around to avoid it. I also give no fucks about any names or labels given to me because of it.
return value is a string and not a DateTime object. - Yes, this is true and for a reason. I even noted that in my comments above the code but you can't see that because I didn't post it.
I think this is the first or second time in my life I have posted code on the Internet for help. I see the majority of people that reply to questions like this attacking everything in the code posted and contributing nothing to the answer of the original question. This is the same as the "Shopping Cart Theory". What I do is no more important than why I do it.
There are 10 types of people. Those that know binary and the ones that don't.
(post is archived)