Text and Date Manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr John Stockton

    #16
    Re: Text and Date Manipulation

    JRS: In article <1110833269.026 789.180050@l41g 2000cwc.googleg roups.com>
    , dated Mon, 14 Mar 2005 12:47:49, seen in news:comp.lang. javascript,
    sgershon@gmail. com posted :[color=blue]
    >When any of the first two changes (to a date in the format
    >"dd/mm/yyyy"), the third one should contain the difference between the
    >dates, in days.
    >
    >Any help with these?[/color]

    Read the newsgroup FAQ.

    Don't use dd/mm/yyyy in an international medium; use yyyy-mm-dd or
    yyyy/mm/dd.

    See below.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

    Comment

    • Dr John Stockton

      #17
      Re: Text and Date Manipulation

      JRS: In article <aaadncUUg9dEj6 vfRVn-tw@comcast.com> , dated Mon, 14 Mar
      2005 16:40:26, seen in news:comp.lang. javascript, McKirahan
      <News@McKirahan .com> posted :
      [color=blue]
      >Will this get you started? Watch for word-wrap.[/color]

      That's your responsibility. Remember that if you hope to have more than
      one reader it will be more efficient for you to get it right than for
      your readers to have to detect and remove the errors that you would have
      introduced. Ensure that what you transmit across the Net can be
      executes in that state; readers then only have to avoid letting their
      own systems introduce error.

      [color=blue]
      >This uses Mick White's formula for "days".[/color]

      Don't publicly copy code that you have not tested yourself, adequately;
      since most code is wrong, that wastes everybody's time.
      [color=blue]
      > var dat2 = form.date2.valu e;
      > if (dat2.length != 10) return;[/color]

      That's a rather crude validation.
      if (!/^\d\d\/\d\d\/\d{4}$/.test(dat2)) return
      at least means, IIRC, that javascript will accept the value as some
      date. For proper date validation, see the FAQ & below.
      [color=blue]
      > form.days.value = Math.floor((new Date(dat2)-new
      >Date(dat1))/(1000*60*60*24) );[/color]

      The OP specified dd/mm/yyyy. For at least some users, and I know of no
      exceptions, new Date() will take it as being mm/dd/yyyy .

      As a general rule, all code using 1000*60*60*24 is highly suspect. Not
      that the product is intrinsically wrong; but those who are smart enough
      to avoid the awaiting error will generally be smart enough to put 864e5
      instead.

      The rule holds in this case. Testing :
      03/09/2005 04/09/2005 30 days difference
      but March has 31 days
      05/09/2005 06/09/2005 31 days difference
      May also has 31 days

      There will with that code be a visible error for every span that
      includes more Springs than Autumns, and an invisible one for those with
      the opposite unbalance, for many but not all users.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • Dr John Stockton

        #18
        Re: Text and Date Manipulation

        JRS: In article <MPG.1c9fc0a966 44d83698a36c@nn tp.lucent.com>, dated
        Mon, 14 Mar 2005 15:39:58, seen in news:comp.lang. javascript, kaeli
        <tiny_one@NOSPA M.comcast.net> posted :[color=blue]
        >
        >Hints:
        >-- onChange
        >-- http://www.w3schools.com/js/js_obj_date.asp[/color]

        That page is Copyright 1999-2005 by Refsnes Data. Therefore, it really
        ought to be correct by now. It is not.

        Ignoring follies such as writing "The getUTCDate method returns the
        Universal Coordinated Time", I see the following clear errors :


        "millisecon ds - the number of milliseconds since 01 January, 1970
        00:00:00" - needs GMT.

        "yr_num, mo_num, day_num - the year, month or day of the date" - is it
        not mo_num-1 ?

        "getFullYea r() Returns the year of a Date object (four digits)" - no, it
        returns a Number in the range -271821-04-20 to +275760; if the Date
        Object represents a date in 1000-9999, that Number requires a four-digit
        string.

        "getYear() Returns the year of a Date object (from 0-99). Use
        getFullYear instead !!" - Generally only 0-99 for 1900-1999

        "getTime() Returns the number of milliseconds since midnight 1/1-1970" -
        again two errors : needs GMT, and which midnight?

        "getTimezoneOff set() Returns the time difference between the user's
        computer and GMT" - does not state units, does not define sign clearly.

        "getUTCFullYear () Returns the four-digit year of a Date object in
        universal time" - not necessarily four.

        "parse() Returns a string date value that holds the number of
        milliseconds since January 01 1970 00:00:00" - needs GMT; in IE4 at
        least, gives a Number.

        "setFullYea r() Sets the year in the Date object (four digits) - not
        necessarily four, and more parameters can be given.

        "setHours() Sets the hour in the Date object (from 0-23)" - more
        parameters can be given.

        "setYear() Sets the year in the Date object (00-99)" - actually sets
        1900-1999 for that; other values in range set properly.


        I may have missed some.


        As a response to a memory test, it's not bad; but as a reference
        document it is inadequate.

        BTW, it only refers to NN & IE; no reference to others.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • Dr John Stockton

          #19
          Re: Text and Date Manipulation

          JRS: In article <0dwZd.333$Zn.2 2398@news.optus .net.au>, dated Tue, 15
          Mar 2005 07:33:16, seen in news:comp.lang. javascript, RobG
          <rgqld@iinet.ne t.auau> posted :[color=blue]
          >SGershon wrote:
          >[...][color=green]
          >> 2) I am having much trouble trying to use the dd/mm/yyyy format in the
          >> date function.[/color]
          >
          > Here is a revised version of McKirahan's code that does some
          > date validation.[/color]

          Validation using RegExp and a Date Object is MUCH shorter and more
          complete; your code accepts 1-3-2000-9 happily, and gives a difference
          of NaN for 1-3-2000k9 and for 21-O4-2000[*]

          (After the M-1 correction -)

          It also accepts 00-02-2000 & 00-03-2000, giving a difference of 29.

          Your DateStr "dd-mm-yyyy" has the advantage that the result of
          new Date(DateStr) will not be able to be attempted to be believed.

          Your code gives me 30 days from 21-03-2005 to 21-04-2005. I expect that
          it will give you 31 days for those dates. You could try 21-10-2005 to
          21-11-2005, for which the correct answer is again 31 days; I have a
          suspicion that you will also get 31 (Que?); but I expect ACT to get 30.
          Tasmaniacs should try a month earlier, AIUI.

          It gives me 1460 days from 28-03-2002 to 28-03-2006, whereas *between*
          1900 and 2100 there are 1461 days per quadrennium. It will give you and
          the Americans 1461, but for somewhat different reasons.

          [*] Wherever a number may reasonably contain the digit zero, entry of
          either O or o should not give an unduly confusing result!

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • Dr John Stockton

            #20
            Re: Text and Date Manipulation

            JRS: In article <C92dnQtDkJV4ba vfRVn-vQ@comcast.com> , dated Tue, 15 Mar
            2005 08:27:50, seen in news:comp.lang. javascript, McKirahan
            <News@McKirahan .com> posted :[color=blue]
            >
            >It's widely recommended that you use the unambiguous ccyy/mm/dd format.
            >[/color]

            No, it is not.

            It is widely recommended that yyyy-mm-dd be used, since that is the
            International Standard; or yyyy/mm/dd because that's similar and
            accepted directly by new Date().

            I have heard that use of "ccyy" was specified in some organisation such
            as the Texas Highway Patrol. Those are, evidently, educated persons who
            know that the 20th century was 1901-2000 (or possibly they thought that
            it was 1900-1999), and they therefore entered the number of the century
            followed by the two-digit number of the year within the century.
            Therefore, they were issuing tickets for dates 100 years ahead ...


            But it could be worse; I have heard that IBM uses a ccyy such that the
            1900s have CC=0 ....

            Stick to yyyy; ccyy has no advantages.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
            Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
            PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
            Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

            Comment

            • Dag Sunde

              #21
              Re: Text and Date Manipulation

              "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
              news:PfzPrSAR72 NCFwbA@merlyn.d emon.co.uk...[color=blue]
              > JRS: In article <MPG.1c9fc0a966 44d83698a36c@nn tp.lucent.com>, dated
              > Mon, 14 Mar 2005 15:39:58, seen in news:comp.lang. javascript, kaeli
              > <tiny_one@NOSPA M.comcast.net> posted :[color=green]
              > >
              > >Hints:
              > >-- onChange
              > >-- http://www.w3schools.com/js/js_obj_date.asp[/color]
              >
              > That page is Copyright 1999-2005 by Refsnes Data. Therefore, it really
              > ought to be correct by now. It is not.
              >
              > Ignoring follies such as writing "The getUTCDate method returns the
              > Universal Coordinated Time", I see the following clear errors :
              >[/color]

              At the bottom of almost every page at w3schools is the following:

              "We Help You For Free. You Can Help Us!
              ....
              Help us correct errors and broken links
              Help us with spelling and grammar
              ....
              (The above are hyperlinks...)

              Why don't you help them getting better, instead of criticising them
              for having wrong or outdated information?

              --
              Dag.


              Comment

              • RobG

                #22
                Re: Text and Date Manipulation

                Dr John Stockton wrote:
                [...][color=blue]
                >
                > Stick to yyyy; ccyy has no advantages.
                >[/color]

                I always presumed that the 'c' stood for the Roman numeral for
                '100', and therefore presumed that if that format is to be used
                at all, it should be 'MCXy' - or in long-hand English:

                'thousands hundreds tens units of years'.

                Which would confuse the heck out of those who don't plain laugh
                at the pseudo-intelligentsia of it all - bah gum.


                --
                Rob

                Comment

                • SGershon

                  #23
                  Re: Text and Date Manipulation

                  Thanks for the selection tip. Worked great.

                  I want to use dd/mm/yyyy, because the date is intended for local
                  audience, not worldwide users. People here are used to dd/mm/yyyy, and
                  I don't want to confuse them.
                  I prefer to confuse javascript and myself...

                  I tried the faq and the merlyn pages, but I could not find any help.

                  Comment

                  • SGershon

                    #24
                    Re: Text and Date Manipulation

                    Thanks for the helping hand.

                    As in a previous post, the audience of the site is local, and I don't
                    want them to be confused with the dating.
                    Standard here is d/m/yyyy, and I am pushing towards dd/mm/yyyy, because
                    that is a minor change.

                    Maybe I need to check the string, and then strip the digits and
                    re-construct the date inside the calculation function... but it seems
                    such a wrong option! It should be a way to tell date() that the format
                    in use is another.

                    SGershon

                    Comment

                    • SGershon

                      #25
                      Re: Text and Date Manipulation

                      Hi all.
                      Just updating you all.

                      In the end, I am doing the not so elegant re-construction of the date
                      format inside the calculation function.
                      I still think that there might be a better and correct solution, but I
                      believe that time will learn it to me.

                      What I mean is, that after ensuring the date format received, I do:
                      var dat1f =
                      dat1.substring( 3,5)+\"/\"+dat1.substri ng(0,2)+\"/\"+dat1.substri ng(6);

                      Thanks again for the helping tips!

                      SGershon

                      Comment

                      • Dr John Stockton

                        #26
                        Re: Text and Date Manipulation

                        JRS: In article <1111359135.596 895.251810@f14g 2000cwb.googleg roups.com>
                        , dated Sun, 20 Mar 2005 14:52:15, seen in news:comp.lang. javascript,
                        SGershon <sgershon@gmail .com> posted :[color=blue]
                        >
                        >What I mean is, that after ensuring the date format received, I do:
                        >var dat1f =
                        >dat1.substring (3,5)+\"/\"+dat1.substri ng(0,2)+\"/\"+dat1.substri ng(6);
                        >[/color]

                        That can more elegantly be done by something like

                        var dat1f = dat1.replace(/(..).(..).(.... )/, "$2/$1/$3")
                        or
                        var dat1f = dat1.replace(/(\d+)\D+(\d+)\D +(\d+)/, "$2/$1/$3")

                        <URL:http://www.merlyn.demo n.co.uk/js-dates.htm>
                        <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                        Comment

                        • SGershon

                          #27
                          Re: Text and Date Manipulation

                          Hey!
                          Thanks, it is much nicer, as it makes clear to any one reading the code
                          what I am intending to do.
                          The way I was using does the same, but was confusing.

                          SGershon

                          Comment

                          Working...