Formatting DateTime Values

  • Thread starter Thread starter Bruce Vander Werf
  • Start date Start date
B

Bruce Vander Werf

I need to format a DateTime in the following format:

"Mmm dd hh:mm:ss"

Where Mmm is the three-letter abbreviation for the month.

What's the best way to do this in C#?

--Bruce
 
Bruce Vander Werf said:
I need to format a DateTime in the following format:

"Mmm dd hh:mm:ss"

Where Mmm is the three-letter abbreviation for the month.

What's the best way to do this in C#?

Use either String.Format or DateTime.ToString:

using System;

public class Test
{
static void Main()
{
DateTime dt = DateTime.Now;

string firstExample = String.Format ("{0:MMM dd hh:mm:ss}",
dt);
string secondExample = dt.ToString ("MMM dd hh:mm:ss");

Console.WriteLine (firstExample);
Console.WriteLine (secondExample);
}
}

You'd usually use the first if you wanted to put it in the middle of
other things.
 
Back
Top