Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Formatting a DateTime object/String to only show yyyy/mm/dd [modified]

Formatting a DateTime object/String to only show yyyy/mm/dd [modified]

Scheduled Pinned Locked Moved C#
question
9 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stephen darling
    wrote on last edited by
    #1

    Hi, I have a DateTime object, and my code correctly sets the year/month and day from a DateTime picker controll on my form. When I return the object and use .ToString, I get the following output:

    19/08/2011 00:00:00

    Now, I am not using or setting the time, so it is always 00:00:00. My question is how do I remove this time part from the string? 1) Is there a method from the DateTime object to do this, or, 2) Do I need to do this once I convert to string, and if so how? Thank you in advance, Regards, Stephen **EDIT Found out. .ToString(format code) **

    modified on Friday, August 19, 2011 8:00 AM

    L OriginalGriffO S 3 Replies Last reply
    0
    • S stephen darling

      Hi, I have a DateTime object, and my code correctly sets the year/month and day from a DateTime picker controll on my form. When I return the object and use .ToString, I get the following output:

      19/08/2011 00:00:00

      Now, I am not using or setting the time, so it is always 00:00:00. My question is how do I remove this time part from the string? 1) Is there a method from the DateTime object to do this, or, 2) Do I need to do this once I convert to string, and if so how? Thank you in advance, Regards, Stephen **EDIT Found out. .ToString(format code) **

      modified on Friday, August 19, 2011 8:00 AM

      L Offline
      L Offline
      loyal ginger
      wrote on last edited by
      #2

      Call

      ToString("MM/dd/yyyy")

      V M 2 Replies Last reply
      0
      • L loyal ginger

        Call

        ToString("MM/dd/yyyy")

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        loyal ginger wrote:

        Call

        ToString("MM/dd/yyyy")

        ToString("dd/MM/yyyy")

        is better in his case I think. :)

        V.

        B 1 Reply Last reply
        0
        • L loyal ginger

          Call

          ToString("MM/dd/yyyy")

          M Offline
          M Offline
          Mirko1980
          wrote on last edited by
          #4

          You could also use the short date format specifier:

          ToString("d")

          1 Reply Last reply
          0
          • S stephen darling

            Hi, I have a DateTime object, and my code correctly sets the year/month and day from a DateTime picker controll on my form. When I return the object and use .ToString, I get the following output:

            19/08/2011 00:00:00

            Now, I am not using or setting the time, so it is always 00:00:00. My question is how do I remove this time part from the string? 1) Is there a method from the DateTime object to do this, or, 2) Do I need to do this once I convert to string, and if so how? Thank you in advance, Regards, Stephen **EDIT Found out. .ToString(format code) **

            modified on Friday, August 19, 2011 8:00 AM

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Just FYI there is a list of the formatting codes here: Formatting a DateTime for display - format string description[^]

            Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            S S 2 Replies Last reply
            0
            • OriginalGriffO OriginalGriff

              Just FYI there is a list of the formatting codes here: Formatting a DateTime for display - format string description[^]

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

              S Offline
              S Offline
              stephen darling
              wrote on last edited by
              #6

              OriginalGriff wrote:

              Just FYI there is a list of the formatting codes here: Formatting a DateTime for display - format string description[^]

              Yes; I found them just after posting. Thank you, Stephen

              1 Reply Last reply
              0
              • V V 0

                loyal ginger wrote:

                Call

                ToString("MM/dd/yyyy")

                ToString("dd/MM/yyyy")

                is better in his case I think. :)

                V.

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #7

                Considering the subject line he probably wants

                ToString("yyyy/MM/dd")

                ;)

                1 Reply Last reply
                0
                • S stephen darling

                  Hi, I have a DateTime object, and my code correctly sets the year/month and day from a DateTime picker controll on my form. When I return the object and use .ToString, I get the following output:

                  19/08/2011 00:00:00

                  Now, I am not using or setting the time, so it is always 00:00:00. My question is how do I remove this time part from the string? 1) Is there a method from the DateTime object to do this, or, 2) Do I need to do this once I convert to string, and if so how? Thank you in advance, Regards, Stephen **EDIT Found out. .ToString(format code) **

                  modified on Friday, August 19, 2011 8:00 AM

                  S Offline
                  S Offline
                  Simon Bang Terkildsen
                  wrote on last edited by
                  #8

                  .ToShortDateString() is also an options

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Just FYI there is a list of the formatting codes here: Formatting a DateTime for display - format string description[^]

                    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                    S Offline
                    S Offline
                    Simon Bang Terkildsen
                    wrote on last edited by
                    #9

                    Very nice tip, bookmarked :thumbsup:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups