Tim Hibbard

CEO for EnGraph software
posts - 629 , comments - 1682 , trackbacks - 459

My Links

News



Add to Google

Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

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:

Print | posted on Thursday, June 26, 2008 3:43 PM | Filed Under [ EnGraph .NET Goldstar ]

Feedback

Gravatar

# re: C# DateTime extension method

Hi Tim, Couldn't you just have done the following:

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

?
6/26/2008 3:43 PM | Pieter
Gravatar

# re: C# DateTime extension method

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!!
6/26/2008 4:01 PM | Tim Hibbard
Gravatar

# re: C# DateTime extension method

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.
6/29/2008 6:30 PM | Chris Chandler
Gravatar

# re: C# DateTime extension method

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];


8/19/2008 12:59 AM | Jonathan Holland
Gravatar

# re: C# DateTime extension method

My Mistake, typed to fast:

return dt.ToShortDateString().PadLeft('0',11);
8/19/2008 1:13 AM | Jonathan Holland
Gravatar

# re: C# DateTime extension method

Just a note: none of the code samples above work on any culture that doesn't use '/' as a date seperator.
9/17/2008 7:18 AM | ruben
Gravatar

# re: C# DateTime extension method

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.
9/23/2010 9:24 AM | hermes birkin
Gravatar

# re: C# DateTime extension method

Nice extension method. Unfortunately there is no need for it! haha
3/13/2013 12:55 AM | Clara Simonis
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: