Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software
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 on Thursday, June 26, 2008 3:43 PM EnGraph , .NET , Goldstar | Back to top


Comments on this post: C# DateTime extension method

# re: C# DateTime extension method
Requesting Gravatar...
Hi Tim, Couldn't you just have done the following:

dt.ToString("MM/dd/yyyy")

?
Left by Pieter on Jun 26, 2008 3:43 PM

# re: C# DateTime extension method
Requesting Gravatar...
Hey Pieter,

Yes I could have. And I would have, but I never remember that you can do that with DateTime. Maybe the embarrassment of writing this blog post will serve as a future reminder.

But hey, those extension methods sure are slick!!
Left by Tim Hibbard on Jun 26, 2008 4:01 PM

# re: C# DateTime extension method
Requesting Gravatar...
I'm not sure this is a good use of extension methods. Formatting is better for patterns, as suggested above. Extension methods are cool, but I do feel cheated that you can't just change the meaning of ToShortDateString() to do what you would like it to do. I mean if you can change an object graph from another object, why not give us the real power, instead of making it just syntactic sugar for a static helper method.
Left by Chris Chandler on Jun 29, 2008 6:30 PM

# re: C# DateTime extension method
Requesting Gravatar...
Lets pretend for a moment that DateTime.ToString() did not exist.

Did it cross your mind that there might be a handy string method just for this purpose?

String[] tokens = dt.ToShortDateString('/');
return tokens[0].PadLeft(2,'0') + "/" + tokens[1] + "/" + tokens[2];


Left by Jonathan Holland on Aug 19, 2008 12:59 AM

# re: C# DateTime extension method
Requesting Gravatar...
My Mistake, typed to fast:

return dt.ToShortDateString().PadLeft('0',11);
Left by Jonathan Holland on Aug 19, 2008 1:13 AM

# re: C# DateTime extension method
Requesting Gravatar...
Just a note: none of the code samples above work on any culture that doesn't use '/' as a date seperator.
Left by ruben on Sep 17, 2008 7:18 AM

# re: C# DateTime extension method
Requesting Gravatar...
Yes I could have. And I would have, but I never remember that you can do that with DateTime. Maybe the embarrassment of writing this blog post will serve as a future reminder.
Left by hermes birkin on Sep 23, 2010 9:24 AM

# re: C# DateTime extension method
Requesting Gravatar...
Nice extension method. Unfortunately there is no need for it! haha
Left by Clara Simonis on Mar 13, 2013 12:55 AM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net | Join free