Tim Hibbard

Software Architect for EnGraph software


News





Add to Google





My Stats

  • Posts - 593
  • Comments - 397
  • Trackbacks - 507

Twitter












Tag Cloud


Recent Comments


Recent Posts


Article Categories


Archives


Post Categories


Image Galleries


EnGraph Blogs


Links


Other


Roll



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 | Filed Under [ EnGraph .NET Goldstar ]

Comments

Gravatar # re: C# DateTime extension method
Posted by Pieter on 6/26/2008 3:43 PM
Hi Tim, Couldn't you just have done the following:

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

?
Gravatar # re: C# DateTime extension method
Posted by Tim Hibbard on 6/26/2008 4:01 PM
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!!
Gravatar # re: C# DateTime extension method
Posted by Chris Chandler on 6/29/2008 6:30 PM
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.
Gravatar # re: C# DateTime extension method
Posted by Jonathan Holland on 8/19/2008 12:53 AM
Wow, you deleted all three of my comments where I refactored your three useless blogpost methods down to one or two lines each.

Way to go, apparently you can't take criticism.


Gravatar # re: C# DateTime extension method
Posted by Jonathan Holland on 8/19/2008 12:59 AM
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];



Gravatar # re: C# DateTime extension method
Posted by Jonathan Holland on 8/19/2008 1:13 AM
My Mistake, typed to fast:

return dt.ToShortDateString().PadLeft('0',11);
Gravatar # re: C# DateTime extension method
Posted by Joe Chung on 8/19/2008 2:11 AM
I don't think it's a good thing when the only reason I found your blog was because people were making fun of a code snippet you wrote in 2006

http://www.reddit.com/comments/6wx6a/ask_reddit_whats_the_funniest_code_youve_ever_read/c053bho

And you're still making the same sorts of mistakes in 2008.
Gravatar # re: C# DateTime extension method
Posted by anonymous coward on 8/19/2008 3:21 AM
You have management written all over you. You should leave the petty coding to us induhviduals.
Gravatar # re: C# DateTime extension method
Posted by daniel on 8/19/2008 1:18 PM
Wow, it makes me sad that, because I live in Brazil, this "Architect" probably makes 10 times what I do.
Post a comment





 

Please add 5 and 2 and type the answer here: