开发者

Get directory name based on current date

开发者 https://www.devze.com 2023-01-17 13:27 出处:网络
I have to get directory name made by following pattern: t开发者_开发百科wo last digits of a year concatenated with month number ( always two digits). For example directory from september 2010 would be

I have to get directory name made by following pattern: t开发者_开发百科wo 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!


This should be quite easy. Use

date.ToString("yyMM");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号