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;
}
}
(post is archived)