format string from 8 to 08 for August.

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

    format string from 8 to 08 for August.

    I need to format a string to it always has 2 digits in it. I'm getting the
    month like this:

    DateTime.Now.Mo nth.ToString()

    Right now since it's August, this returns a string of "8", however, I need
    to return a string of "08". Is there a format method what will
    automatically pad this for me?

    Thanks.


    --
    moondaddy@newsg roup.nospam


  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: format string from 8 to 08 for August.

    On Aug 22, 3:19 pm, "moondaddy" <moonda...@news group.nospamwro te:
    I need to format a string to it always has 2 digits in it.  I'm gettingthe
    month like this:
    >
    DateTime.Now.Mo nth.ToString()
    >
    Right now since it's August, this returns a string of "8", however, I need
    to return a string of "08".  Is there a format method what will
    automatically pad this for me?
    >
    Thanks.
    >
    --
    moonda...@newsg roup.nospam

    use DateTime.Now.To String("MM");

    take a look at the different DateTime format strnig options

    Comment

    • zacks@construction-imaging.com

      #3
      Re: format string from 8 to 08 for August.

      On Aug 22, 3:33 pm, "Ignacio Machin ( .NET/ C# MVP )"
      <ignacio.mac... @gmail.comwrote :
      On Aug 22, 3:19 pm, "moondaddy" <moonda...@news group.nospamwro te:
      >
      I need to format a string to it always has 2 digits in it.  I'm getting the
      month like this:
      >
      DateTime.Now.Mo nth.ToString()
      >
      Right now since it's August, this returns a string of "8", however, I need
      to return a string of "08".  Is there a format method what will
      automatically pad this for me?
      >
      Thanks.
      >
      --
      moonda...@newsg roup.nospam
      >
      use DateTime.Now.To String("MM");
      >
      take a look at the different DateTime format strnig options
      Or string.Format(" {0:D2}", DateTime.Now.Mo nth)

      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: format string from 8 to 08 for August.

        zacks@construct ion-imaging.com wrote:
        On Aug 22, 3:33 pm, "Ignacio Machin ( .NET/ C# MVP )"
        <ignacio.mac... @gmail.comwrote :
        >On Aug 22, 3:19 pm, "moondaddy" <moonda...@news group.nospamwro te:
        >>I need to format a string to it always has 2 digits in it. I'm getting the
        >>month like this:
        >>DateTime.Now. Month.ToString( )
        >>Right now since it's August, this returns a string of "8", however, I need
        >>to return a string of "08". Is there a format method what will
        >>automatical ly pad this for me?
        >use DateTime.Now.To String("MM");
        >>
        >take a look at the different DateTime format strnig options
        >
        Or string.Format(" {0:D2}", DateTime.Now.Mo nth)
        But the .ToString seems both more readable and more
        flexible.

        Arne

        Comment

        • maximz2005

          #5
          Re: format string from 8 to 08 for August.

          On Aug 22, 4:49 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
          za...@construct ion-imaging.com wrote:
          On Aug 22, 3:33 pm, "Ignacio Machin ( .NET/ C# MVP )"
          <ignacio.mac... @gmail.comwrote :
          On Aug 22, 3:19 pm, "moondaddy" <moonda...@news group.nospamwro te:
          >I need to format a string to it always has 2 digits in it.  I'm getting the
          >month like this:
          >DateTime.Now.M onth.ToString()
          >Right now since it's August, this returns a string of "8", however, Ineed
          >to return a string of "08".  Is there a format method what will
          >automaticall y pad this for me?
          use DateTime.Now.To String("MM");
          >
          take a look at the different DateTime format strnig options
          >
          Or string.Format(" {0:D2}", DateTime.Now.Mo nth)
          >
          But the .ToString seems both more readable and more
          flexible.
          >
          Arne
          You can do it either way, it formats the string the way your format
          string specifies.

          Read up on string formatting. It's very interesting. Try experimenting
          with string formatting by using the string.Format() function. There
          are lists of format codes on the web, just google for format string C#.

          Comment

          • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

            #6
            Re: format string from 8 to 08 for August.

            maximz2005 wrote:
            On Aug 22, 4:49 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
            >za...@construc tion-imaging.com wrote:
            >>On Aug 22, 3:33 pm, "Ignacio Machin ( .NET/ C# MVP )"
            >><ignacio.mac. ..@gmail.comwro te:
            >>>On Aug 22, 3:19 pm, "moondaddy" <moonda...@news group.nospamwro te:
            >>>>I need to format a string to it always has 2 digits in it. I'm getting the
            >>>>month like this:
            >>>>DateTime.No w.Month.ToStrin g()
            >>>>Right now since it's August, this returns a string of "8", however, I need
            >>>>to return a string of "08". Is there a format method what will
            >>>>automatical ly pad this for me?
            >>>use DateTime.Now.To String("MM");
            >>>take a look at the different DateTime format strnig options
            >>Or string.Format(" {0:D2}", DateTime.Now.Mo nth)
            >But the .ToString seems both more readable and more
            >flexible.
            >
            You can do it either way, it formats the string the way your format
            string specifies.
            No one has denied that.

            Arne

            Comment

            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

              #7
              Re: format string from 8 to 08 for August.

              moondaddy wrote:
              I need to format a string to it always has 2 digits in it. I'm getting the
              month like this:
              >
              DateTime.Now.Mo nth.ToString()
              >
              Right now since it's August, this returns a string of "8", however, I need
              to return a string of "08". Is there a format method what will
              automatically pad this for me?
              >
              Thanks.
              >
              Here are some ways of doing it (including the ones already mentioned in
              the thread):

              DateTime.Now.Mo nth.ToString("0 0")
              DateTime.Now.Mo nth.ToString("D 2")
              DateTime.Now.To String("MM")
              string.Format(" {0:00}", DateTime.Now.Mo nth)
              string.Format(" {0:D2}", DateTime.Now.Mo nth)
              string.Format(" {0:MM}", DateTime.Now)

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              • moondaddy

                #8
                Re: format string from 8 to 08 for August.

                Thanks Göran and everyone else who replied. this has all been very helpful.


                "Göran Andersson" <guffa@guffa.co mwrote in message
                news:uG$itBZBJH A.3484@TK2MSFTN GP04.phx.gbl...
                moondaddy wrote:
                >I need to format a string to it always has 2 digits in it. I'm getting
                >the month like this:
                >>
                >DateTime.Now.M onth.ToString()
                >>
                >Right now since it's August, this returns a string of "8", however, I
                >need to return a string of "08". Is there a format method what will
                >automaticall y pad this for me?
                >>
                >Thanks.
                >>
                >
                Here are some ways of doing it (including the ones already mentioned in
                the thread):
                >
                DateTime.Now.Mo nth.ToString("0 0")
                DateTime.Now.Mo nth.ToString("D 2")
                DateTime.Now.To String("MM")
                string.Format(" {0:00}", DateTime.Now.Mo nth)
                string.Format(" {0:D2}", DateTime.Now.Mo nth)
                string.Format(" {0:MM}", DateTime.Now)
                >
                --
                Göran Andersson
                _____
                http://www.guffa.com

                Comment

                Working...