Get directory name based on current date
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");
精彩评论