WelcomeUser Guide
ToSPrivacyCanary
DonateBugsLicense

©2024 Poal.co

854

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 (edited )

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;
                }
            }













[+] [deleted] 0 pt