Ah, bad date handling. We've all seen it. We all know it. So when Lorenzo sent us this C# function, we almost ignored it:

private string GetTimeStamp(DateTime param)
{
    string retDate = param.Year.ToString() + "-";
    if (param.Month < 10)
        retDate = retDate + "0" + param.Month.ToString() + "-";
    else
        retDate = retDate + param.Month.ToString() + "-";

    if (param.Day < 10)
        retDate = retDate + "0" + param.Day.ToString() + " ";
    else
        retDate = retDate + param.Day.ToString() + " ";

    if (param.Hour < 10)
        retDate = retDate + "0" + param.Hour.ToString() + ":";
    else
        retDate = retDate + param.Hour.ToString() + ":";

    if (param.Minute < 10)
        retDate = retDate + "0" + param.Minute.ToString() + ":";
    else
        retDate = retDate + param.Minute.ToString() + ":";

    if (param.Second < 10)
        retDate = retDate + "0" + param.Second.ToString() + ".";
    else
        retDate = retDate + param.Second.ToString() + ".";

    if (param.Millisecond < 10)
        retDate = retDate + "0" + param.Millisecond.ToString();
    else
        retDate = retDate + param.Millisecond.ToString();

    return retDate;
}

Most of this function isn't terribly exciting. We've seen this kind of bad code before, but even when we see a repeat like this, there are still special treats in it. Look at the section for handling milliseconds: if the number is less than 10, they pad it with a leading zero. Just the one, though. One leading zero should be enough for everybody.

But that's not the thing that makes this code special. You see, there's another function worth looking at:

private string FileTimeStamp(DateTime param)
{
    string retDate = param.Year.ToString() + "-";
    if (param.Month < 10)
        retDate = retDate + "0" + param.Month.ToString() + "-";
    else
        retDate = retDate + param.Month.ToString() + "-";

    if (param.Day < 10)
        retDate = retDate + "0" + param.Day.ToString() + " ";
    else
        retDate = retDate + param.Day.ToString() + " ";

    if (param.Hour < 10)
        retDate = retDate + "0" + param.Hour.ToString() + ":";
    else
        retDate = retDate + param.Hour.ToString() + ":";

    if (param.Minute < 10)
        retDate = retDate + "0" + param.Minute.ToString() + ":";
    else
        retDate = retDate + param.Minute.ToString() + ":";

    if (param.Second < 10)
        retDate = retDate + "0" + param.Second.ToString() + ".";
    else
        retDate = retDate + param.Second.ToString() + ".";

    if (param.Millisecond < 10)
        retDate = retDate + "0" + param.Millisecond.ToString();
    else
        retDate = retDate + param.Millisecond.ToString();

    return retDate;
}

Not only did they fail to learn the built-in functions for formatting dates, they forgot about the functions they wrote for formatting dates, and just wrote (or realistically, copy/pasted?) the same function twice.

At least both versions have the same bug with milliseconds. I don't know if I could handle it if they were inconsistent about that.

[Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.