date code and string reverse

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

    #1

    date code and string reverse

    I have a need to reverse a date to show as a "date code". For example
    today, 090507 would be coded as 905070 (reversing the parts of the date.

    This is what i have but there has to be a better way.

    DateTime d = DateTime.Now;
    string datecodemonth = d.ToString("MM" );
    datecodemonth = datecodemonth[1] + datecodemonth[0];
    string datecodeday = d.ToString("dd" );
    datecodeday = datecodeday[1] + datecodeday[0];
    string datecodeyear = d.ToString("yy" );
    datecodeyear = datecodeyear[1] + datecodeyear[0];
    string datecode = datecodemonth + datecodeday + datecodeyear;

    Ideas? Is there anything in the format of the date that will do this?

    dan
  • Marc Gravell

    #2
    Re: date code and string reverse

    Seems a very odd thing to want to do, but why not ;-p
    If this is high volume, then to avoid lots of intermediate strings,
    try to work in a char[] buffer, something like:

    static void Main(string[] args) {
    char[] buffer =
    DateTime.Today. ToString("MMddy y").ToCharArray ();
    Swap(buffer, 0, 1); // perhaps unroll...
    Swap(buffer, 2, 3);
    Swap(buffer, 4, 5);
    string s = new string(buffer);
    Debug.WriteLine (s);
    }
    static void Swap(char[] data, int index1, int index2) {
    char c = data[index1];
    data[index1] = data[index2];
    data[index2] = c;
    }

    Comment

    • Mark Rae [MVP]

      #3
      Re: date code and string reverse

      "Dan Holmes" <danholmes@bigf oot.comwrote in message
      news:OUCboUA8HH A.536@TK2MSFTNG P06.phx.gbl...
      Ideas? Is there anything in the format of the date that will do this?
      Firstly, create a function to reverse a string:

      string StringReverse(s tring pstrString)
      {
      char[] strArray = pstrString.ToCh arArray();
      Array.Reverse(s trArray);
      return new string(strArray );
      }

      Then do the following:

      string strDateLiteral = "090507";
      DateTime dtmDate = DateTime.ParseE xact(strDateLit eral, "MMddyy", null);
      string strDateReversed =
      StringReverse(d tmDate.ToString ("MM")) +
      StringReverse(d tmDate.ToString ("dd")) +
      StringReverse(d tmDate.ToString ("yy"));


      --
      Mark Rae
      ASP.NET MVP


      Comment

      • UL-Tomten

        #4
        Re: date code and string reverse


        On Sep 5, 11:39 pm, Dan Holmes <danhol...@bigf oot.comwrote:
        I have a need to reverse a date to show as a "date code". For example
        today, 090507 would be coded as 905070 (reversing the parts of the date.
        Both Mark's and Marc's examples use the default DateTime string format
        provider, and then manipulates its results. A more .NET-ty solution
        might be to implement a custom format provider, and use that to create
        a string from a DateTime instead of the default provider. This would
        avoid first going to 090507 and then to 905070, going directly to
        905070 from a DateTime instance. See here for an example on how to
        implement a custom format provider: http://www.codeproject.com/csharp/custstrformat.asp

        Comment

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

          #5
          Re: date code and string reverse

          Dan Holmes wrote:
          I have a need to reverse a date to show as a "date code". For example
          today, 090507 would be coded as 905070 (reversing the parts of the date.
          >
          This is what i have but there has to be a better way.
          >
          DateTime d = DateTime.Now;
          string datecodemonth = d.ToString("MM" );
          datecodemonth = datecodemonth[1] + datecodemonth[0];
          string datecodeday = d.ToString("dd" );
          datecodeday = datecodeday[1] + datecodeday[0];
          string datecodeyear = d.ToString("yy" );
          datecodeyear = datecodeyear[1] + datecodeyear[0];
          string datecode = datecodemonth + datecodeday + datecodeyear;
          >
          Ideas? Is there anything in the format of the date that will do this?
          You can do this with string operations or perhaps with a custom format
          provider. I would however go for a numeric method, as the source is numeric.

          Something like:

          DateTime d = DateTime.Now;
          int year = d.Year % 100;
          int month = d.Month;
          int day = d.Day;
          string dateCode =
          (month % 10).ToString() +
          (month / 10).ToString() +
          (day % 10).ToString() +
          (day / 10).ToString() +
          (year % 10).ToString() +
          (year / 10).ToString();

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

          Comment

          • Doug Semler

            #6
            Re: date code and string reverse

            "Göran Andersson" <guffa@guffa.co mwrote in message
            news:ORuDvNF8HH A.4476@TK2MSFTN GP06.phx.gbl...
            Dan Holmes wrote:
            >I have a need to reverse a date to show as a "date code". For example
            >today, 090507 would be coded as 905070 (reversing the parts of the date.
            >>
            >This is what i have but there has to be a better way.
            >>
            >DateTime d = DateTime.Now;
            >string datecodemonth = d.ToString("MM" );
            >datecodemont h = datecodemonth[1] + datecodemonth[0];
            >string datecodeday = d.ToString("dd" );
            >datecodeday = datecodeday[1] + datecodeday[0];
            >string datecodeyear = d.ToString("yy" );
            >datecodeyear = datecodeyear[1] + datecodeyear[0];
            >string datecode = datecodemonth + datecodeday + datecodeyear;
            >>
            >Ideas? Is there anything in the format of the date that will do this?
            >
            You can do this with string operations or perhaps with a custom format
            provider. I would however go for a numeric method, as the source is
            numeric.
            >
            Something like:
            >
            DateTime d = DateTime.Now;
            int year = d.Year % 100;
            int month = d.Month;
            int day = d.Day;
            string dateCode =
            (month % 10).ToString() +
            (month / 10).ToString() +
            (day % 10).ToString() +
            (day / 10).ToString() +
            (year % 10).ToString() +
            (year / 10).ToString();
            >

            StringBuilder people...String Builder!!!

            --
            Doug Semler, MCPD
            a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
            The answer is 42; DNRC o-
            Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
            erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

            Comment

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

              #7
              Re: date code and string reverse

              Doug Semler wrote:
              "Göran Andersson" <guffa@guffa.co mwrote in message
              news:ORuDvNF8HH A.4476@TK2MSFTN GP06.phx.gbl...
              >Dan Holmes wrote:
              >>I have a need to reverse a date to show as a "date code". For
              >>example today, 090507 would be coded as 905070 (reversing the parts
              >>of the date.
              >>>
              >>This is what i have but there has to be a better way.
              >>>
              >>DateTime d = DateTime.Now;
              >>string datecodemonth = d.ToString("MM" );
              >>datecodemon th = datecodemonth[1] + datecodemonth[0];
              >>string datecodeday = d.ToString("dd" );
              >>datecodeday = datecodeday[1] + datecodeday[0];
              >>string datecodeyear = d.ToString("yy" );
              >>datecodeyea r = datecodeyear[1] + datecodeyear[0];
              >>string datecode = datecodemonth + datecodeday + datecodeyear;
              >>>
              >>Ideas? Is there anything in the format of the date that will do this?
              >>
              >You can do this with string operations or perhaps with a custom format
              >provider. I would however go for a numeric method, as the source is
              >numeric.
              >>
              >Something like:
              >>
              >DateTime d = DateTime.Now;
              >int year = d.Year % 100;
              >int month = d.Month;
              >int day = d.Day;
              >string dateCode =
              > (month % 10).ToString() +
              > (month / 10).ToString() +
              > (day % 10).ToString() +
              > (day / 10).ToString() +
              > (year % 10).ToString() +
              > (year / 10).ToString();
              >>
              >
              >
              StringBuilder people...String Builder!!!
              >
              Why? A StringBuilder doesn't perform better, and there is no scalability
              issues here.

              If you want performance, this is four times faster:

              DateTime d = DateTime.Now;
              int year = d.Year % 100;
              int month = d.Month;
              int day = d.Day;
              char[] c = new char[6];
              c[0] = (char)((month % 10) + 48);
              c[1] = (char)((month / 10) + 48);
              c[2] = (char)((day % 10) + 48);
              c[3] = (char)((day / 10) + 48);
              c[4] = (char)((year % 10) + 48);
              c[5] = (char)((year / 10) + 48);
              return new string(c);

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

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: date code and string reverse

                On Sep 7, 8:14 am, Göran Andersson <gu...@guffa.co mwrote:
                Doug Semler wrote:
                "Göran Andersson" <gu...@guffa.co mwrote in message
                news:ORuDvNF8HH A.4476@TK2MSFTN GP06.phx.gbl...
                Dan Holmes wrote:
                >I have a need to reverse a date to show as a "date code". For
                >example today, 090507 would be coded as 905070 (reversing the parts
                >of the date.
                >
                >This is what i have but there has to be a better way.
                >
                >DateTime d = DateTime.Now;
                >string datecodemonth = d.ToString("MM" );
                >datecodemont h = datecodemonth[1] + datecodemonth[0];
                >string datecodeday = d.ToString("dd" );
                >datecodeday = datecodeday[1] + datecodeday[0];
                >string datecodeyear = d.ToString("yy" );
                >datecodeyear = datecodeyear[1] + datecodeyear[0];
                >string datecode = datecodemonth + datecodeday + datecodeyear;
                >
                >Ideas? Is there anything in the format of the date that will do this?
                >
                You can do this with string operations or perhaps with a custom format
                provider. I would however go for a numeric method, as the source is
                numeric.
                >
                Something like:
                >
                DateTime d = DateTime.Now;
                int year = d.Year % 100;
                int month = d.Month;
                int day = d.Day;
                string dateCode =
                (month % 10).ToString() +
                (month / 10).ToString() +
                (day % 10).ToString() +
                (day / 10).ToString() +
                (year % 10).ToString() +
                (year / 10).ToString();
                >
                StringBuilder people...String Builder!!!
                >
                Why? A StringBuilder doesn't perform better, and there is no scalability
                issues here.
                >
                If you want performance, this is four times faster:
                >
                DateTime d = DateTime.Now;
                int year = d.Year % 100;
                int month = d.Month;
                int day = d.Day;
                char[] c = new char[6];
                c[0] = (char)((month % 10) + 48);
                c[1] = (char)((month / 10) + 48);
                c[2] = (char)((day % 10) + 48);
                c[3] = (char)((day / 10) + 48);
                c[4] = (char)((year % 10) + 48);
                c[5] = (char)((year / 10) + 48);
                return new string(c);
                It only comes out 2.3 times faster for me, but there we go. This
                version is between the two, and I prefer it for readability:

                DateTime d = DateTime.Now;
                string original = d.ToString("yyd dMM");
                char[] c = original.ToChar Array();
                Array.Reverse(c );
                return new string(c);

                Jon

                Comment

                • Marc Gravell

                  #9
                  Re: date code and string reverse

                  [Smacks head] Why didn't I thnk of that!

                  Marc


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: date code and string reverse

                    Doug Semler <dougsemler@gma il.comwrote:
                    The compiler, in cases such as the one above, can optimize alot of the
                    instantiations out of the picture that you wouldn't see. This is because,
                    even though a string is treated as an Object, at runtime it is treated as a
                    Value class (struct).
                    No, it is certainly *not* treated as a struct. It's treated as a
                    reference type in every possible way.
                    Therefore, the compiler can actually perform compile
                    time value optimizations on strings. A simple example:
                    >
                    string MyFunc(int a, int b)
                    {
                    string sa = a.ToString();
                    string sb = b.ToString();
                    return sa + sb;
                    }
                    >
                    The compiler is smart enough to convert that into a concatination that is
                    back filled (basically what happens is an array of strings is created with
                    all of the "ToString" calls, on FINAL string is created, and the characters
                    of each string are then filled into the final string).
                    Um, any evidence of that?

                    I believe that 3 completely separate strings will be created in the
                    above code. There's no special optimisation going on as far as I'm
                    aware.

                    --
                    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

                    • Marc Gravell

                      #11
                      Re: date code and string reverse

                      Um, any evidence of that?
                      I agree with Jon; the real demonstration here is when you have more
                      than 2 strings:

                      string d = a + b + c; // 3 existing strings

                      The point is, it /doesn't/ evaluate the left-to-right, which is what
                      you might reasonably expect, but which would create intermediate
                      strings (i.e. tmp = a+b, d = tmp + c). Instead the complier swaps it
                      for:

                      string d = string.Concat(a ,b,c);

                      Internally, string.Concat finds the sum of all the strings, allocates
                      a buffer big enough for all 3, and inserts the provided strings into
                      the buffer one after the other. Generally, you would have to use a
                      char[] buffer for this, but since the new string hasn't seen the
                      outside world yet, the runtime actually uses a string directly and
                      edits the (otherwise immutable) string directly.

                      Marc

                      Comment

                      • Marc Gravell

                        #12
                        Re: date code and string reverse

                        is there expert opinion (yourselves included of course!) on when
                        to use String and when to use StringBuilder?
                        Jon's page is (as usual) quite illuminating:
                        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


                        Basically, if you are concatenating strings in a loop, then
                        StringBuilder is your friend. If you are concatenating strings in a
                        single line (i.e. someVar + "alkfj" + someOtherVar + whatever + "
                        items"), then just let the compiler do the work. You can also use
                        StringBuilder to retain editability of the contents while
                        concatenating, but I've never had to use this (I prefer to simply
                        build the correct string as I go)).
                        If you are writing an über-string (perhaps file contents, CSV
                        maybe...) then a third approach is to use a TextWriter to write
                        directly to an output Stream (such as a file, or an HttpResponse).
                        This can be even more efficient, as the working data is minimised -
                        very useful for server work or volume processing.

                        Marc

                        Comment

                        Working...