Formatting a String to a Date String

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

    Formatting a String to a Date String

    This is driving me mad, should be simple,

    How can I format a date string "01122006" to be "01/12/2006"

    I have tried
    string teststr = String.Format(" {d}","01122006" );

    AND

    string teststr = String.Format(" {##/##/####","01122006 ");

    and various other methods but these throw errors.

    Can someone help please ?

    Thanks

    Jon.


  • Ciaran O''Donnell

    #2
    RE: Formatting a String to a Date String

    You need to make it a datetime first, Try:

    DateTime.ParseE xact("01122006" , "ddMMyyyy",
    DateTimeFormatI nfo.CurrentInfo ).ToString("dd/MM/yyyy");

    Ciaran O'Donnell



    "Bishman" wrote:
    This is driving me mad, should be simple,
    >
    How can I format a date string "01122006" to be "01/12/2006"
    >
    I have tried
    string teststr = String.Format(" {d}","01122006" );
    >
    AND
    >
    string teststr = String.Format(" {##/##/####","01122006 ");
    >
    and various other methods but these throw errors.
    >
    Can someone help please ?
    >
    Thanks
    >
    Jon.
    >
    >
    >

    Comment

    • Gustavo Ringel

      #3
      Re: Formatting a String to a Date String

      if you set sDate = "01122006"
      You may try
      String.Format(" {0}/{1}/{2}",sDate.subs tring(0,2),sDat e.substring(2,2 ),sDate.substri ng(4,4))

      Gustavo.

      "Bishman" <jonathan.bisho p@btinternet.co mwrote in message
      news:ehQQSEKCHH A.5068@TK2MSFTN GP02.phx.gbl...
      This is driving me mad, should be simple,
      >
      How can I format a date string "01122006" to be "01/12/2006"
      >
      I have tried
      string teststr = String.Format(" {d}","01122006" );
      >
      AND
      >
      string teststr = String.Format(" {##/##/####","01122006 ");
      >
      and various other methods but these throw errors.
      >
      Can someone help please ?
      >
      Thanks
      >
      Jon.
      >
      >

      Comment

      • Bishman

        #4
        Re: Formatting a String to a Date String

        Thanks for the replies,

        I must have got mixed up with other types of formatting !

        I will give these a go,

        Jon.



        "Bishman" <jonathan.bisho p@btinternet.co mwrote in message
        news:ehQQSEKCHH A.5068@TK2MSFTN GP02.phx.gbl...
        This is driving me mad, should be simple,
        >
        How can I format a date string "01122006" to be "01/12/2006"
        >
        I have tried
        string teststr = String.Format(" {d}","01122006" );
        >
        AND
        >
        string teststr = String.Format(" {##/##/####","01122006 ");
        >
        and various other methods but these throw errors.
        >
        Can someone help please ?
        >
        Thanks
        >
        Jon.
        >
        >

        Comment

        • Rad [Visual C# MVP]

          #5
          RE: Formatting a String to a Date String

          ParseExact is the way to go. However if you want to manipulate the day month
          and year separately (catering for different cultures) you could to it this
          way:

          //
          // Create a regex
          //
          Regex r = new Regex(@"(?<day> \d{2})(?<month> \d{2})(?<year>\ d{4})");
          //
          // Extract the values
          //
          Match m = r.Match("010220 06");
          int day = int.Parse(m.Gro ups["day"].Value);
          int month = int.Parse(m.Gro ups["month"].Value);
          int year = int.Parse(m.Gro ups["year"].Value);
          //
          // Now that we have the components separately,
          // Construct the datetime
          //
          DateTime date= new DateTime(year,m onth,day);

          --


          Bits. Bytes.

          ------------------------------


          "Bishman" wrote:
          This is driving me mad, should be simple,
          >
          How can I format a date string "01122006" to be "01/12/2006"
          >
          I have tried
          string teststr = String.Format(" {d}","01122006" );
          >
          AND
          >
          string teststr = String.Format(" {##/##/####","01122006 ");
          >
          and various other methods but these throw errors.
          >
          Can someone help please ?
          >
          Thanks
          >
          Jon.
          >
          >
          >

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Formatting a String to a Date String

            In my idea the only good answer until now,

            All other answers are converting from string to a DateTime structure.

            :-)

            Cor

            "Gustavo Ringel" <gustavo.ringel @gmail.comschre ef in bericht
            news:uSZizTKCHH A.996@TK2MSFTNG P02.phx.gbl...
            if you set sDate = "01122006"
            You may try
            String.Format(" {0}/{1}/{2}",sDate.subs tring(0,2),sDat e.substring(2,2 ),sDate.substri ng(4,4))
            >
            Gustavo.
            >
            "Bishman" <jonathan.bisho p@btinternet.co mwrote in message
            news:ehQQSEKCHH A.5068@TK2MSFTN GP02.phx.gbl...
            >This is driving me mad, should be simple,
            >>
            >How can I format a date string "01122006" to be "01/12/2006"
            >>
            >I have tried
            >string teststr = String.Format(" {d}","01122006" );
            >>
            >AND
            >>
            >string teststr = String.Format(" {##/##/####","01122006 ");
            >>
            >and various other methods but these throw errors.
            >>
            >Can someone help please ?
            >>
            >Thanks
            >>
            >Jon.
            >>
            >>
            >
            >

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Formatting a String to a Date String

              Cor Ligthert [MVP] <notmyfirstname @planet.nlwrote :
              In my idea the only good answer until now,
              >
              All other answers are converting from string to a DateTime structure.
              >
              :-)
              It's entirely reasonable to convert to a DateTime structure *and then
              format that*, just as in Ciaran's response.

              The problem with *not* going via a DateTime is that you have no error
              checking. For instance, Gustavo's code would convert "abcdef" into
              "ab/cd/ef" instead of (correctly, IMO) noticing that the string isn't a
              valid date.

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • Mihai N.

                #8
                Re: Formatting a String to a Date String

                In my idea the only good answer until now,
                >
                All other answers are converting from string to a DateTime structure.
                >
                >:-)
                >
                Cor
                Sorry, but going thru a DateTime is the best thing.
                Then you can format it the way you want. And ##/##/## is not how you want,
                for a properly internationaliz ed application.


                --
                Mihai Nita [Microsoft MVP, Windows - SDK]
                Best internationalization practices, solving internationalization problems.

                ------------------------------------------
                Replace _year_ with _ to get the real email

                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: Formatting a String to a Date String

                  It's entirely reasonable to convert to a DateTime structure *and then
                  format that*, just as in Ciaran's response.
                  >
                  The problem with *not* going via a DateTime is that you have no error
                  checking. For instance, Gustavo's code would convert "abcdef" into
                  "ab/cd/ef" instead of (correctly, IMO) noticing that the string isn't a
                  valid date.
                  The advance with *not* going to a DateTime structure is that you have not
                  any problem with inbuild verification as in the DateTime structure as by
                  instance check on leapmonths, way of writting or whatever. There are
                  situations where people ignore that expressely with good reasons.

                  If you want the advantages of a DateTime structure, than you should use a
                  DateTime structure from the beginning not only to set a position extra.

                  Cor

                  Cor


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Formatting a String to a Date String

                    Cor Ligthert [MVP] <notmyfirstname @planet.nlwrote :
                    It's entirely reasonable to convert to a DateTime structure *and then
                    format that*, just as in Ciaran's response.

                    The problem with *not* going via a DateTime is that you have no error
                    checking. For instance, Gustavo's code would convert "abcdef" into
                    "ab/cd/ef" instead of (correctly, IMO) noticing that the string isn't a
                    valid date.
                    >
                    The advance with *not* going to a DateTime structure is that you have not
                    any problem with inbuild verification as in the DateTime structure as by
                    instance check on leapmonths, way of writting or whatever. There are
                    situations where people ignore that expressely with good reasons.
                    I can't remember ever being in the situation where I *want*, say,
                    Februrary 29th 2005 to be a valid input. Can you give an example where
                    that's the case?
                    If you want the advantages of a DateTime structure, than you should use a
                    DateTime structure from the beginning not only to set a position extra.
                    What do you mean by "not only to set a position extra"?

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    • Jay

                      #11
                      Re: Formatting a String to a Date String

                      When importing data from an old DOS programme, I once had invalid
                      dates like February 29th 2005. I reasoned that since people were
                      happy with them in the original programme, they would be happy
                      with them in the new one too. Turning them into a valid date would
                      have been very tricky, trying to second guess what the user intended.
                      I can't remember ever being in the situation where I *want*, say,
                      Februrary 29th 2005 to be a valid input. Can you give an example
                      where that's the case?


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: Formatting a String to a Date String

                        Jay wrote:
                        When importing data from an old DOS programme, I once had invalid
                        dates like February 29th 2005. I reasoned that since people were
                        happy with them in the original programme, they would be happy
                        with them in the new one too. Turning them into a valid date would
                        have been very tricky, trying to second guess what the user intended.
                        Fair enough. I would hope that situations like that are a rarity rather
                        than a common requirement though :)

                        Jon

                        Comment

                        • Cor Ligthert [MVP]

                          #13
                          Re: Formatting a String to a Date String

                          29 february is not in a leap month it is a date in a leapyear in the
                          Gregorian calendar, a leapmonth is something else in the Hebrewic calendar,
                          I was sure that you would give an answer like you did.



                          The rest is answered by Jay.

                          Cor

                          "Jon Skeet [C# MVP]" <skeet@pobox.co mschreef in bericht
                          news:MPG.1fc624 33f7a10f8898d61 6@msnews.micros oft.com...
                          Cor Ligthert [MVP] <notmyfirstname @planet.nlwrote :
                          It's entirely reasonable to convert to a DateTime structure *and then
                          format that*, just as in Ciaran's response.
                          >
                          The problem with *not* going via a DateTime is that you have no error
                          checking. For instance, Gustavo's code would convert "abcdef" into
                          "ab/cd/ef" instead of (correctly, IMO) noticing that the string isn't a
                          valid date.
                          >>
                          >The advance with *not* going to a DateTime structure is that you have not
                          >any problem with inbuild verification as in the DateTime structure as by
                          >instance check on leapmonths, way of writting or whatever. There are
                          >situations where people ignore that expressely with good reasons.
                          >
                          I can't remember ever being in the situation where I *want*, say,
                          Februrary 29th 2005 to be a valid input. Can you give an example where
                          that's the case?
                          >
                          >If you want the advantages of a DateTime structure, than you should use a
                          >DateTime structure from the beginning not only to set a position extra.
                          >
                          What do you mean by "not only to set a position extra"?
                          >
                          --
                          Jon Skeet - <skeet@pobox.co m>
                          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                          If replying to the group, please do not mail me too

                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: Formatting a String to a Date String

                            Cor Ligthert [MVP] <notmyfirstname @planet.nlwrote :
                            29 february is not in a leap month it is a date in a leapyear in the
                            Gregorian calendar, a leapmonth is something else in the Hebrewic calendar,
                            I was sure that you would give an answer like you did.
                            >

                            >
                            The rest is answered by Jay.
                            So in a few very specific cases, you'd want different validation, or
                            perhaps no validation at all. That's fine, but I don't think that's
                            *nearly* as common a requirement as "normal" date checking, which is
                            where using DateTime is the way to go.

                            --
                            Jon Skeet - <skeet@pobox.co m>
                            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                            If replying to the group, please do not mail me too

                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: Formatting a String to a Date String

                              You think I know.

                              Cor

                              "Jon Skeet [C# MVP]" <skeet@pobox.co mschreef in bericht
                              news:MPG.1fc6c2 d564cc671e98d61 8@msnews.micros oft.com...
                              Cor Ligthert [MVP] <notmyfirstname @planet.nlwrote :
                              >29 february is not in a leap month it is a date in a leapyear in the
                              >Gregorian calendar, a leapmonth is something else in the Hebrewic
                              >calendar,
                              >I was sure that you would give an answer like you did.
                              >>
                              >http://en.wikipedia.org/wiki/Hebrew_calendar
                              >>
                              >The rest is answered by Jay.
                              >
                              So in a few very specific cases, you'd want different validation, or
                              perhaps no validation at all. That's fine, but I don't think that's
                              *nearly* as common a requirement as "normal" date checking, which is
                              where using DateTime is the way to go.
                              >
                              --
                              Jon Skeet - <skeet@pobox.co m>
                              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                              If replying to the group, please do not mail me too

                              Comment

                              Working...