C#
Trying to get a date out of a string. For some reason this is not working. Could be I'm tired and just don't see it.
The date it should get is 2017-10-05 but it does not detect a date at all.
file = "171005_010"
f = Regex.Replace(file, "[^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;
}
}
EDIT
Found the problem.
changed
f = Regex.Replace(file, "[^0-9]", "");
to
f = Regex.Replace(f, "[^0-9]", "");
file did NOT = "171005_010"
Almost never fails. Just have to ask before I figure it out.
C#
Trying to get a date out of a string. For some reason this is not working. Could be I'm tired and just don't see it.
The date it should get is 2017-10-05 but it does not detect a date at all.
```
file = "171005_010"
f = Regex.Replace(file, "[^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;
}
}
```
EDIT
Found the problem.
changed
`f = Regex.Replace(file, "[^0-9]", "");`
to
`f = Regex.Replace(f, "[^0-9]", "");`
file did NOT = "171005_010"
Almost never fails. Just have to ask before I figure it out.
(post is archived)