tags:

views:

33

answers:

1

I have to get directory name made by following pattern: two last digits of a year concatenated with month number ( always two digits). For example directory from september 2010 would be "1009".

I did it but I found my code quite trashy. How can I improve it?

My current code:

    public string GetDirectoryNameFromDate(DateTime date)
    {
        StringBuilder sb = new StringBuilder();

        sb.Append(date.Year.ToString().Substring(2));

        int month = date.Month;
        if (month < 10)
        {
            sb.Append("0");
        }
        sb.Append(month.ToString());

        return sb.ToString();
    }

Thanks for advice!

+4  A: 

This should be quite easy. Use

date.ToString("yyMM");
Øyvind Bråthen