changing date format

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

    #1

    changing date format

    Hello,

    i'm using a form with 2 dates in the format mm/dd/yyyy in it. I wanna
    calculate the days between the two dates.
    I tried it like this:

    <script language="JavaS cript">
    var date1 = new date("10/22/2003");
    var date2 = new date("10/25/2003");
    var diff = date2.gettime() - date1.gettime() ;
    </script>

    only it seems that the date format like this won't work is there anything i
    can do to convert the format or convience it to use the date like that?

    Thanks for the help
    chris


  • Thomas 'PointedEars' Lahn

    #2
    Re: changing date format

    Chris wrote:
    [color=blue]
    > i'm using a form with 2 dates in the format mm/dd/yyyy in it. I wanna
    > calculate the days between the two dates. I tried it like this:
    >
    > <script language="JavaS cript">[/color]

    That's invalid HTML 4, the `type' attribute is missing:

    http://validator.w3.org/
    [color=blue]
    > var date1 = new date("10/22/2003"); var date2 = new date("10/25/2003");[/color]

    This won't work anyway or if it does, it works most certainly only for you:
    JavaScript is case-sensitive, the constructor function for Date objects is
    Date(...), not date(...).
    [color=blue]
    > var diff = date2.gettime() - date1.gettime() ;
    > </script>
    >
    > only it seems that the date format like this won't work[/color]

    Of course not, see

    [color=blue]
    > is there anything i can do to convert the format or convience it to use
    > the date like that?[/color]

    See the discussion with the subject `Date datatype' one week ago.


    PointedEars

    Comment

    • Dr John Stockton

      #3
      Re: changing date format

      JRS: In article <vpc9a692abn805 @corp.supernews .com>, seen in
      news:comp.lang. javascript, Chris <cmohn@gmx.de > posted at Wed, 22 Oct
      2003 02:35:49 :-
      [color=blue]
      >i'm using a form with 2 dates in the format mm/dd/yyyy in it. I wanna
      >calculate the days between the two dates.[/color]
      [color=blue]
      >var date1 = new date("10/22/2003");
      >var date2 = new date("10/25/2003");
      >var diff = date2.gettime() - date1.gettime() ;[/color]


      Date differencing is covered in <URL:http://www.merlyn.demon.co.uk/js-
      date2.htm>; read the rest of the Javascript material there too.

      The 'word' "wanna" is not good English; if you can, please use proper
      English, out of courtesy to those who are trying to learn it. Much the
      same goes for "i'm"; case is important in proper English, as it is for
      Javascript. For example, "new Date(...)".

      You seem to be German; you should not therefore want to give dates in
      DDF. While (AIUI) currently most browsers interpret 10/22/2002 as Oct
      22, 2003, one should not rely on that, since it can readily be avoided
      by using the proper form "2003/10/22" with ISO-8601 field order, or by
      using "2003 Oct 22". I have not heard of any browser that does not
      accept those. Note that while 04/07/2003 will be interpreted as April
      7th by present browsers, a European reader may think that it means 4th
      July.

      Method gettime does not exist; getTime() returns milliseconds. The
      number of milliseconds in a Javascript day is not always 864E5; you
      should test for, say the cases of 2003/03/31 - 2003/03/28 and 2003/10/27
      - 2003/10/24 (still assuming that you are in the EU).

      Just date2 - date1 would suffice.

      If the input is from a user, then you should consider validating the
      dates. Code like you give would accept "13/13/2003" as meaning
      2004-01-13.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      Working...