Need of a code snipet which converts mm/dd/yy to dd/mm/yy

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

    Need of a code snipet which converts mm/dd/yy to dd/mm/yy

    Hi ,
    I am stuck with a requirement from my client to change the date
    format from mm/dd/yy to dd/mm/yy .If any body can help me out with
    this regard as its very much urgent.




    Regards,
    Santanu
  • Captain Paralytic

    #2
    Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

    On 22 Apr, 11:02, santanu mishra <mishra.sant... @gmail.comwrote :
    Hi ,
    I am stuck with a requirement from my client to change the date
    format from mm/dd/yy to dd/mm/yy .If any body can help me out with
    this regard as its very much urgent.
    >
    Regards,
    Santanu
    I use:

    Comment

    • Gregor Kofler

      #3
      Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

      santanu mishra meinte:
      Hi ,
      I am stuck with a requirement from my client to change the date
      format from mm/dd/yy to dd/mm/yy .If any body can help me out with
      this regard as its very much urgent.
      >
      Untested:

      function convert(dateStr ing) {
      var new = dateString.spli t("/");
      return new[1]+"/"+new[0]+"/"+new[2];
      }

      Gregor


      --
      http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
      http://web.gregorkofler.com ::: meine JS-Spielwiese
      http://www.image2d.com ::: Bildagentur für den alpinen Raum

      Comment

      • Stevo

        #4
        Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

        Gregor Kofler wrote:
        santanu mishra meinte:
        > I am stuck with a requirement from my client to change the date
        >format from mm/dd/yy to dd/mm/yy .If any body can help me out with
        >this regard as its very much urgent.
        >
        Untested:
        function convert(dateStr ing) {
        var new = dateString.spli t("/");
        return new[1]+"/"+new[0]+"/"+new[2];
        }
        If that doesn't work, try replacing the variable name new with a
        different name. I wouldn't be surprised if that's a problem.

        Comment

        • Gregor Kofler

          #5
          Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

          Stevo meinte:
          Gregor Kofler wrote:
          >santanu mishra meinte:
          >> I am stuck with a requirement from my client to change the date
          >>format from mm/dd/yy to dd/mm/yy .If any body can help me out with
          >>this regard as its very much urgent.
          >>
          >Untested:
          >function convert(dateStr ing) {
          > var new = dateString.spli t("/");
          > return new[1]+"/"+new[0]+"/"+new[2];
          >}
          >
          If that doesn't work, try replacing the variable name new with a
          different name. I wouldn't be surprised if that's a problem.
          Yes, my mistake. Something like "newDateStr ing" is definitely better,
          than a reserved word.

          Gregor

          --
          http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
          http://web.gregorkofler.com ::: meine JS-Spielwiese
          http://www.image2d.com ::: Bildagentur für den alpinen Raum

          Comment

          • Evertjan.

            #6
            Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

            Gregor Kofler wrote on 22 apr 2008 in comp.lang.javas cript:
            Stevo meinte:
            >Gregor Kofler wrote:
            >>santanu mishra meinte:
            >>> I am stuck with a requirement from my client to change the date
            >>>format from mm/dd/yy to dd/mm/yy .If any body can help me out with
            >>>this regard as its very much urgent.
            >>>
            >>Untested:
            >>function convert(dateStr ing) {
            >> var new = dateString.spli t("/");
            >> return new[1]+"/"+new[0]+"/"+new[2];
            >>}
            >>
            >If that doesn't work, try replacing the variable name new with a
            >different name. I wouldn't be surprised if that's a problem.
            >
            Yes, my mistake. Something like "newDateStr ing" is definitely better,
            than a reserved word.
            function convert(d) {
            return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
            };

            or

            function convert(d) {
            return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
            };

            both tested IE7.


            --
            Evertjan.
            The Netherlands.
            (Please change the x'es to dots in my emailaddress)

            Comment

            • Stevo

              #7
              Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

              Evertjan. wrote:
              Gregor Kofler wrote on 22 apr 2008 in comp.lang.javas cript:
              >
              >Stevo meinte:
              >>Gregor Kofler wrote:
              >>>santanu mishra meinte:
              >>>> I am stuck with a requirement from my client to change the date
              >>>>format from mm/dd/yy to dd/mm/yy .If any body can help me out with
              >>>>this regard as its very much urgent.
              >>>Untested:
              >>>function convert(dateStr ing) {
              >>> var new = dateString.spli t("/");
              >>> return new[1]+"/"+new[0]+"/"+new[2];
              >>>}
              >>If that doesn't work, try replacing the variable name new with a
              >>different name. I wouldn't be surprised if that's a problem.
              >Yes, my mistake. Something like "newDateStr ing" is definitely better,
              >than a reserved word.
              >
              function convert(d) {
              return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
              };
              >
              or
              >
              function convert(d) {
              return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
              };
              >
              both tested IE7.
              I'd stick with what Gregor created. It's simple, efficient and easy to
              understand by whoever gets to maintain the code in the future.

              Comment

              • Evertjan.

                #8
                Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

                Stevo wrote on 22 apr 2008 in comp.lang.javas cript:
                >function convert(d) {
                > return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
                >};
                >>
                >or
                >>
                >function convert(d) {
                > return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
                >};
                >>
                >both tested IE7.
                >
                I'd stick with what Gregor created. It's simple, efficient and easy to
                understand by whoever gets to maintain the code in the future.
                >
                As you wish, Stevo. Why is it efficient?

                The above is simple javascript.
                I wouldn't recomment having my code maintained
                by someone that does not understand the above.

                And like all good functions, it can be seen as a black box from outside.

                You could feel the need to add all kinds of validation for bad input,
                however.

                --
                Evertjan.
                The Netherlands.
                (Please change the x'es to dots in my emailaddress)

                Comment

                • Stevo

                  #9
                  Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

                  Evertjan. wrote:
                  Stevo wrote on 22 apr 2008 in comp.lang.javas cript:
                  >>function convert(d) {
                  >> return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
                  >>};
                  >>or
                  >>function convert(d) {
                  >> return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
                  >>};
                  >>both tested IE7.
                  >I'd stick with what Gregor created. It's simple, efficient and easy to
                  >understand by whoever gets to maintain the code in the future.
                  As you wish, Stevo. Why is it efficient?
                  I just mean that it's simple, efficient ENOUGH, and immediately obvious
                  (without more than 2 seconds of thought) what it's doing. If you know
                  what the split method does, then you know what the function is doing.
                  The above is simple javascript.
                  I wouldn't recomment having my code maintained
                  by someone that does not understand the above.
                  We see almost every day people coming here that are given the task by
                  their employers of changing some code that they don't understand. We
                  might think that's a dumb idea, but it's happening all the time.

                  I'm not a fan of regexp constructs because of their lack of readability
                  to those unfamiliar with them, and that is a large proportion of
                  programmers that just never get into them. I don't mind admitting that I
                  fall into that group too. So the only reason I know what your second
                  function is doing, is because I know what job it has to do from the OP's
                  original requirements. If I didn't have that knowledge, then I'd be
                  wondering what this $2$1 business is all about. I've deduced that what
                  you've got in parentheses is looking for digit, digit, forward-slash and
                  when it finds one it generates the $1 token, then it looks again for
                  digit, digit forward slash and generates the $2 token, then the replace
                  happens and $2 and $1 get swapped in the returned string. If I didn't
                  have those OP requirements, I can guarantee I'd be at wikipedia's
                  regular expression page trying to find out what's going on. Don't get me
                  wrong, I'm impressed with it, but I'd never use it.

                  Comment

                  • Gregor Kofler

                    #10
                    Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

                    Evertjan. meinte:
                    function convert(d) {
                    return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
                    };
                    >
                    or
                    >
                    function convert(d) {
                    return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
                    };
                    >
                    both tested IE7.
                    Agreed. But optimization should be the last step. Besides, I suppose the
                    OP might have problems with understanding the condensed code or the reg
                    ex (which might be slower than the split).

                    Gregor


                    --
                    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                    http://web.gregorkofler.com ::: meine JS-Spielwiese
                    http://www.image2d.com ::: Bildagentur für den alpinen Raum

                    Comment

                    • Tim Streater

                      #11
                      Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

                      In article <fumkbi$a1i$01$ 1@news.t-online.com>, Stevo <no@mail.invali d>
                      wrote:
                      Evertjan. wrote:
                      Stevo wrote on 22 apr 2008 in comp.lang.javas cript:
                      >function convert(d) {
                      > return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
                      >};
                      >or
                      >function convert(d) {
                      > return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
                      >};
                      >both tested IE7.
                      I'd stick with what Gregor created. It's simple, efficient and easy to
                      understand by whoever gets to maintain the code in the future.
                      As you wish, Stevo. Why is it efficient?
                      >
                      I just mean that it's simple, efficient ENOUGH, and immediately obvious
                      (without more than 2 seconds of thought) what it's doing. If you know
                      what the split method does, then you know what the function is doing.
                      >
                      The above is simple javascript.
                      I wouldn't recomment having my code maintained
                      by someone that does not understand the above.
                      >
                      We see almost every day people coming here that are given the task by
                      their employers of changing some code that they don't understand. We
                      might think that's a dumb idea, but it's happening all the time.
                      >
                      I'm not a fan of regexp constructs because of their lack of readability
                      to those unfamiliar with them, and that is a large proportion of
                      programmers that just never get into them. I don't mind admitting that I
                      fall into that group too. So the only reason I know what your second
                      function is doing, is because I know what job it has to do from the OP's
                      original requirements. If I didn't have that knowledge, then I'd be
                      wondering what this $2$1 business is all about. I've deduced that what
                      you've got in parentheses is looking for digit, digit, forward-slash and
                      when it finds one it generates the $1 token, then it looks again for
                      digit, digit forward slash and generates the $2 token, then the replace
                      happens and $2 and $1 get swapped in the returned string. If I didn't
                      have those OP requirements, I can guarantee I'd be at wikipedia's
                      regular expression page trying to find out what's going on. Don't get me
                      wrong, I'm impressed with it, but I'd never use it.
                      I agree 100% with this. I almost never use regexp for just this reason,
                      because they are unreadable. This doesn't stop me writing large apps
                      which are efficient enough.

                      Comment

                      • Dr J R Stockton

                        #12
                        Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

                        In comp.lang.javas cript message <4da1f25e-7bd2-4a20-91ce-9b49d37394dd@v2
                        3g2000pro.googl egroups.com>, Tue, 22 Apr 2008 04:02:39, santanu mishra
                        <mishra.santanu @gmail.composte d:
                        >Hi ,
                        I am stuck with a requirement from my client to change the date
                        >format from mm/dd/yy to dd/mm/yy .If any body can help me out with
                        >this regard as its very much urgent.
                        St = "mm/dd/yy"
                        St1 = St.substring(3, 6) + St.substring(0, 3) + St.substring(6)
                        St2 = St.substr(3,3) + St.substr(0,3) + St.substr(6)
                        St3 = St.replace(/(..).(..)/, "$2/$1")
                        x = [St1, St2, St3].join(" ") // to show results

                        Note that it will also convert "dd/mm/yy" to "mm/dd/yy"; thus it can be
                        sold in the USA.

                        Those who decline to learn simple RegExp use are wasting their own
                        future time, unless they decide to give up programming in favour of a
                        less intellectual pursuit, such as politics.

                        An OP who needs to ask such a question should do likewise.

                        Aside : is there a representation, known to browsers, for the old-style
                        s as in "Ye olde faufage-fhop" in which I used f instead?

                        --
                        (c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike 6.05.
                        Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
                        Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm: about usage of News.
                        No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.

                        Comment

                        • Rik Wasmus

                          #13
                          Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

                          On Wed, 23 Apr 2008 19:27:55 +0200, Dr J R Stockton
                          <jrs@merlyn.dem on.co.ukwrote:
                          Aside : is there a representation, known to browsers, for the old-style
                          s as in "Ye olde faufage-fhop" in which I used f instead?
                          &#x17F; or ſ
                          --
                          Rik Wasmus

                          Comment

                          • Stevo

                            #14
                            Re: Need of a code snipet which converts mm/dd/yy to dd/mm/yy

                            Dr J R Stockton wrote:
                            Those who decline to learn simple RegExp use are wasting their own
                            future time, unless they decide to give up programming in favour of a
                            less intellectual pursuit, such as politics.
                            >
                            An OP who needs to ask such a question should do likewise.
                            Is there any need for that?

                            There are a lot of us that don't consider RegExp a good alternative.

                            Imagine how much code is being run in the background to parse a RegExp
                            string. In this particular example, the code that Gregor provided, which
                            was a simple array split, followed by a simple concatenation, was (a)
                            extremely easy to understand, and (2) doesn't involve very complex
                            regexp parsing in the background. The code that handles the split and
                            concatenation code behind the scenes we can all imagine is fairly
                            trivial. The same can't be said for a RegExp parser.

                            I prefer two lines of code that are easy to understand and as far as
                            complexity goes, what you see is pretty much all that's happening. The
                            RegExp version is not obvious if you're unfamiliar, and involves too big
                            an overhead in the background. Certainly too big for this particular
                            case. If you're parsing a humongous length string then the RegExp
                            overhead would pay off because the code you'd have to write wouldn't be
                            just two lines.

                            I have used RegExp code before, I always fully comment what it does
                            though, and if the alternative (as in this case) is a better solution,
                            then I'll always prefer that.

                            Comment

                            • John G Harris

                              #15
                              faufage-fhop

                              On Wed, 23 Apr 2008 at 18:27:55, in comp.lang.javas cript, Dr J R
                              Stockton wrote:

                              <snip>
                              >Aside : is there a representation, known to browsers, for the old-style
                              s as in "Ye olde faufage-fhop" in which I used f instead?
                              The HTML character entities don't include a long s, but they do include
                              the integral sign, &int; , which might do instead. It works in IE5.5+.
                              Failing that, you'll have to use a small image instead.

                              Incidentally, some early English printing displayed 'the' as a y with a
                              dot over it. Presumably a hand-written 'ye' was often abbreviated that
                              way.

                              John
                              --
                              John Harris

                              Comment

                              Working...