WelcomeUser Guide
ToSPrivacyCanary
DonateBugsLicense

©2024 Poal.co

232

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)

[–] 0 pt

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.

[+] [deleted] 0 pt