Tim Hibbard

Software Architect for EnGraph software


News





Add to Google





My Stats

  • Posts - 593
  • Comments - 387
  • Trackbacks - 507

Twitter












Tag Cloud


Recent Comments


Recent Posts


Article Categories


Archives


Post Categories


Image Galleries


EnGraph Blogs


Links


Other


Roll


June 2008 Entries

C# DateTime extension method


Currently DateTime.ToShortDateString will not include any "0" prefixes. I think this makes any vertical lists of dates look funky:
Here is a very simple extension method that will append any needed leading "0" to the date:
namespace ParaPlan.Extensions
{
public static class DateHelper
{
public static string ToShortDateEqualLengthString(this DateTime dt)
{
var rv = new StringBuilder();
if (dt.Month.ToString().Length ==1)
{
rv.Append("0");
}
rv.Append(dt.Month.ToString());
rv.Append("/");
if (dt.Day.ToString().Length == 1)
{
rv.Append("0");
}
rv.Append(dt.Day.ToString());
rv.Append("/");
rv.Append(dt.Year.ToString());

return rv.ToString();
}
}
}
Changing our dates to call .ToShortDateEqualLengthString will make our listbox look much more pretty:

posted @ Thursday, June 26, 2008 3:43 PM | Feedback (9) | Filed Under [ EnGraph .NET Goldstar ]