Formatting DateTime Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce Vander Werf

    #1

    Formatting DateTime Values


    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

  • Jon Skeet

    #2
    Re: Formatting DateTime Values

    Bruce Vander Werf <brucev2@hotmai l.com> wrote:[color=blue]
    > 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#?[/color]

    Use either String.Format or DateTime.ToStri ng:

    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.WriteLi ne (firstExample);
    Console.WriteLi ne (secondExample) ;
    }
    }

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

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...