loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 



Advertise Here
C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Convert DateTime to Military Time

by naspinski 12/23/2008 4:58:00 AM

Good ol' military time


To continue on my posts about extend existing classes (I have been doing these a lot at my new job), here is a simple extension to make DateTime.ToMilitaryString() to output a military time such as: 21 DEC 08 : 16:22:34 - it's very simple actually:
public static string ToMilitaryString(this DateTime dt)
{
  string time = dt.ToString("dd MMM yy : hh:mm:ss");
  return time.Substring(0,12) + dt.Hour + time.Substring(14,6);
}

Currently rated 2.4 by 7 people

  • Currently 2.428572/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

c# | steal some code

Related posts

Comments

6/29/2009 2:07:12 PM

paper1337
You could do it the easy way...

return dt.ToString("dd MMM yy : HH:mm:ss");

Note the capital "H" rather than the lowercase "h" denoting 24-hour time. See msdn.microsoft.com/en-us/library/8kb3ddd4.aspx for reference.

paper1337 us


Comments are closed