Date Format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Pair

    #1

    Date Format

    Hi,

    I use DateTime.Now to display the current date and time. For instance,
    it's default format is 2003-10-29 21:23:36.

    How can I change the format as follows?

    10/29/2003 9:23:36 PM

    MM/DD/YY HH/MM/SS AM/PM

    Thanks for help

    Tom


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Date Format

    Tom,

    When using the ToString method on the DateTime structure, you can use
    the following format string to get the format that you wish:

    M/D/yyyy h:m:s tt

    If you want to have a leading zero for any of the arguments (month, day,
    hour, minute, second), then double up the format character for that section.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Tom Pair" <kerocow@yahoo. com> wrote in message
    news:%23ssM5qmj DHA.1960@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > Hi,
    >
    > I use DateTime.Now to display the current date and time. For instance,
    > it's default format is 2003-10-29 21:23:36.
    >
    > How can I change the format as follows?
    >
    > 10/29/2003 9:23:36 PM
    >
    > MM/DD/YY HH/MM/SS AM/PM
    >
    > Thanks for help
    >
    > Tom
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]


    Comment

    • Tom

      #3
      Re: Date Format

      Hi,

      I replaced DateTime.Now with DateTime.Now.To String(). But,
      the date is same as before.

      Could you teach me how to get the M/D/yyyy h:m:s tt format
      from that date string?

      Thanks for help

      Tom

      [color=blue]
      >-----Original Message-----
      >Tom,
      >
      > When using the ToString method on the DateTime[/color]
      structure, you can use[color=blue]
      >the following format string to get the format that you[/color]
      wish:[color=blue]
      >
      >M/D/yyyy h:m:s tt
      >
      > If you want to have a leading zero for any of the[/color]
      arguments (month, day,[color=blue]
      >hour, minute, second), then double up the format[/color]
      character for that section.[color=blue]
      >
      > Hope this helps.
      >
      >
      >--
      > - Nicholas Paldino [.NET/C# MVP]
      > - nick(dot)[/color]
      paldino=at=exis consulting<dot> com[color=blue]
      >
      >"Tom Pair" <kerocow@yahoo. com> wrote in message
      >news:%23ssM5qm jDHA.1960@TK2MS FTNGP12.phx.gbl ...[color=green]
      >> Hi,
      >>
      >> I use DateTime.Now to display the current date and[/color][/color]
      time. For instance,[color=blue][color=green]
      >> it's default format is 2003-10-29 21:23:36.
      >>
      >> How can I change the format as follows?
      >>
      >> 10/29/2003 9:23:36 PM
      >>
      >> MM/DD/YY HH/MM/SS AM/PM
      >>
      >> Thanks for help
      >>
      >> Tom
      >>
      >>
      >> *** Sent via Developersdex http://www.developersdex.com[/color][/color]
      ***[color=blue][color=green]
      >> Don't just participate in USENET...get rewarded for it![/color]
      >
      >
      >.
      >[/color]

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Date Format

        Tom,

        I don't understand what you are trying to do. The representation of a
        DateTime structure in .NET is just that, an instance of the DateTime
        structure.

        If you want to get that date in a string that is formatted that way,
        take the string that I posted, and then pass it into the ToString method,
        like this:

        // Get the current date/time in the string format we desire.
        string pstrFormattedDa teTime = DateTime.Now.To String("M/D/yyyy h:m:s tt");


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nick(dot)paldin o=at=exisconsul ting<dot>com

        "Tom" <kerocow@yahoo. com> wrote in message
        news:06bb01c38e 74$f71b0700$a00 1280a@phx.gbl.. .[color=blue]
        > Hi,
        >
        > I replaced DateTime.Now with DateTime.Now.To String(). But,
        > the date is same as before.
        >
        > Could you teach me how to get the M/D/yyyy h:m:s tt format
        > from that date string?
        >
        > Thanks for help
        >
        > Tom
        >
        >[color=green]
        > >-----Original Message-----
        > >Tom,
        > >
        > > When using the ToString method on the DateTime[/color]
        > structure, you can use[color=green]
        > >the following format string to get the format that you[/color]
        > wish:[color=green]
        > >
        > >M/D/yyyy h:m:s tt
        > >
        > > If you want to have a leading zero for any of the[/color]
        > arguments (month, day,[color=green]
        > >hour, minute, second), then double up the format[/color]
        > character for that section.[color=green]
        > >
        > > Hope this helps.
        > >
        > >
        > >--
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - nick(dot)[/color]
        > paldino=at=exis consulting<dot> com[color=green]
        > >
        > >"Tom Pair" <kerocow@yahoo. com> wrote in message
        > >news:%23ssM5qm jDHA.1960@TK2MS FTNGP12.phx.gbl ...[color=darkred]
        > >> Hi,
        > >>
        > >> I use DateTime.Now to display the current date and[/color][/color]
        > time. For instance,[color=green][color=darkred]
        > >> it's default format is 2003-10-29 21:23:36.
        > >>
        > >> How can I change the format as follows?
        > >>
        > >> 10/29/2003 9:23:36 PM
        > >>
        > >> MM/DD/YY HH/MM/SS AM/PM
        > >>
        > >> Thanks for help
        > >>
        > >> Tom
        > >>
        > >>
        > >> *** Sent via Developersdex http://www.developersdex.com[/color][/color]
        > ***[color=green][color=darkred]
        > >> Don't just participate in USENET...get rewarded for it![/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        • Tom Pair

          #5
          Re: Date Format

          I tried to use DateTime.Now.To String("M/D/yyyy h:m:s tt")

          The time is correct. But, the date prints 10-D-2003.

          If I use DateTime.Now.To String("m/d/yyyy h:m:s tt")

          The date prints 39-10-2003.

          If I use DateTime.Now.To String("M/d/yyyy h:m:s tt")

          The date prints 10-10-2003.

          How can the date print 10/10/2003? or, it is no way to do it?

          Thanks for your help.







          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • muscha

            #6
            Re: Date Format

            > How can the date print 10/10/2003? or, it is no way to do it?

            Use this:

            dd/MM/yyyy

            Use the "exact" spelling, including upper and lower case.

            /m


            Comment

            Working...